gjaldon / ecto_enum

Ecto extension to support enums in models
MIT License
562 stars 131 forks source link

Enum array can contain null values or an empty array #107

Open Sgoettschkes opened 4 years ago

Sgoettschkes commented 4 years ago

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:

EctoEnum.defenum(
  TestApp.MyEnum,
  :my_enum_type,
  [:one, :two, :three]
)

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.