[Proposal] Ecto.Changeset.validate_change/3 optionally validate nil

7 views
Skip to first unread message

Grant West

unread,
Jun 19, 2024, 9:48:55 PMJun 19
to elixir-ecto
Ecto.Changeset.validate_change/3 from the documentation we see:

It invokes the validator function to perform the validation only if a change for the given field exists and the change value is not nil. The function must return a list of errors (with an empty list meaning no errors).

In my use case I would like to do some validation when a field is being set to nil. I would like to check the value of other field(s) before allowing this field to be set to nil. Normally I could use a DB constraint, but in this case I am checking against a virtual field. 

I am wondering if anyone else thinks making validate_change optionally validate nil would be useful. 

And then that brings up validate_required. Setting a field to nil does not satisfy validate_required. It seems if we were to allow validate_change to optionally validate nil changes, then we would also need to optionally have setting a field to nil satisfy validate_required

José Valim

unread,
Jun 20, 2024, 2:10:30 AMJun 20
to elixi...@googlegroups.com
Hi Grant, validate_change is meant to be a function for the most common use cases. For other scenarios, you can always do the validation yourself and call add_error at the end.

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/b1511acd-e550-46a0-af41-4c754b6e1e7dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages