Validating constructor pattern?

Tags: #<Tag:0x00007f113d810330> #<Tag:0x00007f113d8101a0> #<Tag:0x00007f113d810010>


Is there a convention/idiom for defining structs that require validation on construction?

I notice the standard library types Date and Time define a new function that returns an error tuple when the inputs are invalid:

{:error, :invalid_date}

Is there any downside to declaring structs as ecto embedded_schema with a new/1 function that returns an error tuple? eg:

defmodule MyStruct do
  use Ecto.Schema
  alias Ecto.Changeset

  @primary_key false
  embedded_schema do 
    field :a, :integer
    field :b, :string

  def new(attrs) do
    case cs = changeset(%MyStruct{}, attrs) do
      %{valid?: true} -> {:ok, Changeset.apply_changes(cs)}
      _ -> {:error, changeset_errors(cs)}

  defp changeset(data = %MyStruct{}, attrs) do
    |> Changeset.cast(attrs, [:a, :b])
    |> Changset.validate_required([:a, :b])
    |> Changeset.validate_format(:b, ~r/@/)

  defp changeset_errors(changeset) do
    Changeset.traverse_errors(changeset, fn {msg, opts} ->
      Enum.reduce(opts, msg, fn {key, value}, acc ->
        String.replace(acc, "%{#{key}}", to_string(value))


Indeed, using a new function, combined with declaring the struct’s type as @opaque (as hint that people should not construct it directly and in many cases not pattern match on it directly either) is the convention that is used for this. As for the return value in case of an error, there is no convention for this, (I have seen all of %MyStruct{} | nil, %MyStruct{} | {:error, reason} and {:ok, %MyStruct{}} | {:error, reason}; I like the third the most myself, but it depends on the situation).

As for the example with the Ecto changeset: I am not sure this is the proper way. It feels like you are partially re-inventing the logic that changesets themselves provide, but I am not immediately sure how to improve on it.

It feels more natural to me to have return a version of the struct with sensible default values (without any filled in ‘attrs’) and explicitly fill in the changeset with attrs at the place where you use it, i.e:

defmodule MyStructContext do
  def create_mystruct(attrs) do
    |> creation_changeset(attrs)
    |> Repo.insert()

  def creation_changeset(mystruct = %MyStruct, attrs) do
    |> MyStruct.common_changeset(attrs) # Ensures that invariants of MyStruct are never invalidated.
    |> Changeset.cast(attrs, [:someting, :only_applicable_on_creation])
    |> Changeset.validate_required([:something, :only_applicable_on_creation])


Thanks @Qqwy !

I’m not so much concerned with structs that will be used with a Repo, as %Changeset{} or %Multi{} works really well in those cases.

But for cases where you might otherwise use defstruct, is embedded_schema a suitable alternative, for the convenience of Changeset.cast and Changeset.validate_* functions?


you can use straight maps with Changeset. If you need to validate you can always validate a map and create struct from the map afterwards.


Thats neat!

However I like the idea of making structs composable with embeds_one / embeds_many which I don’t think is supported with schemaless changesets?


I use validate_change for the embedded maps, which takes function as validator. This way you can compose validations pretty much for any imaginable structure.