It is not covered in docs. I tried this, but without success
iex> {%{}, %{p: {:parameterized, Ecto.Enum, %{values: [:a]}}}} |> cast(%{p: "a"}, [:p])
#Ecto.Changeset<
action: nil,
changes: %{},
errors: [
p: {"is invalid",
[type: {:parameterized, Ecto.Enum, %{values: [:a]}}, validation: :cast]}
],
data: %{},
valid?: false
>
support this if possible and document how to use
Hvae you tried calling Ecto.Enum.init(...) when setting up the type instead of hardcoding the values directly?
Ecto.Enum.cast relies on the on_dump option. It is defined based on the values option when initializing the field:
iex(9)> Ecto.Enum.init(values: [:a, :b, :c])
%{
on_dump: %{a: "a", b: "b", c: "c"},
on_load: %{"a" => :a, "b" => :b, "c" => :c},
values: [:a, :b, :c]
}
So, I believe your example could be:
iex(11 > {%{}, %{p: {:parameterized, Ecto.Enum, Ecto.Enum.init(values: [:a]}}} |> Ecto.Changeset.cast(%{p: "a"}, [:p])
Ah, thank you! Didn't know about init.
Most helpful comment
Hvae you tried calling
Ecto.Enum.init(...)when setting up the type instead of hardcoding the values directly?