Adzz / data_schema

Declarative schemas for data transformations.
Apache License 2.0
86 stars 9 forks source link

Add option to treat empty lists as nil for the purpose of the optional check #32

Closed Adzz closed 2 years ago

Adzz commented 2 years ago

for has_many and list_of fields if we return an empty list from a data accessor and the field is not optional we don't error.

Arguably we should because it is empty. The problem is for some input types (like maps. json) there may be a difference between "the value was provided and it was []" and "the value does not exist in the input data". This is the difference between: %{preferences: []} and %{}. We don't want to lose this distinction.

In ecto they have an option when casting empty_values which lets the user specify which values we should consider to be empty. If a field has any of those values returned from the data accessor when casting we treat them as nil for the purpose of the optionality check.

I wonder if a similar thing here could be useful...

Adzz commented 2 years ago

In https://github.com/Adzz/data_schema/pull/33 we introduce empty_values and in https://github.com/Adzz/data_schema/pull/36 we introduce defaults.

There is still some improvement, for now defaults are always functions called at runtime, but we could also allow compile time constants.

I'm not fully sure if these options are the best of all worlds - for example they make the fields verbose, but it works for now.