With this code, I can set the field to [:one], [:two, :three], which is correct and the expected behaviour. Setting the field to [:four] or [:one, :four] results in an invalid changeset, which is expected as well. Setting the field to [], [nil] or [:one, nil] results in a valid changeset which I didn't expect to happen. I would expect this values to result in an invalid changeset as well.
I've encountered the following behaviour which I suggest should either be pointed out in the docs or fixed in the code.
Using an EctoEnum within an array of values, the array can be empty or can contain
nil
values.Have an EctoEnum:
Have a field defining an array of EctoEnum:
field(:my_enum, {:array, TestApp.MyEnum}, default: [:one])
Define the migration for the field:
add(:my_enum, {:array, :my_enum_type}, null: false)
With this code, I can set the field to
[:one]
,[:two, :three]
, which is correct and the expected behaviour. Setting the field to[:four]
or[:one, :four]
results in an invalid changeset, which is expected as well. Setting the field to[]
,[nil]
or[:one, nil]
results in a valid changeset which I didn't expect to happen. I would expect this values to result in an invalid changeset as well.