Custom Ecto.Type without database interaction

Is it ok to ignore load/1 , dump/1 and type/0 when creating a custom Ecto.Type that is used to cast values from an html form, but isn’t used directly to persist to the database? It feels a bit icky to return :error for those callbacks.

I wonder if it’s common to have a custom type. without db interaction.

I think it’s a perfectly valid use of Ecto.Type. Ecto is not only about persistence. Ecto.Schema can be used to define embedded schemas used purely for data casting and validation, with no DB interaction. And Ecto.Type can be used to define casting rules for your custom data types, as you’re already doing.

No need to return :error for those callbacks - you can return {:ok, value} for load/1 and dump/1 and the underlying Ecto type (e.g. :string) for type/0.


It’s completely valid to use a custom Ecto.Type without a database – ecto by itself is completely usable without one, that’s why there’s an ecto_sql that deals with DBs :wink: – but I’d still advise you to adhere to the expected return value format and just adapt your consuming code to that.

1 Like

Thx for the reassurance :slight_smile: