Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Adzz opened this issue Jul 28, 2022 · 1 comment
Closed

Comments

@Adzz
Copy link
Owner

Adzz commented Jul 28, 2022

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
Copy link
Owner Author

Adzz commented Oct 11, 2022

In #33 we introduce empty_values and in #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.

@Adzz Adzz closed this as completed Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant