Keats/validator

breaking change to custom function now must accept `&Option<T>`

Closed this issue · 3 comments

The latest validator version v0.17.0 doesn't align with the following behavior any more for custom function.

A validation on an Option<_> field will be executed on the contained type if the option is Some.

The custom function must now accept &Option<T> as its argument if it corresponds to the type of a struct field, in contrast to its previous behavior, which only accepted &T even if the actual field type was Option<T>.

I thought the previous behaviour was a bug (#303) but I guess it makes more sense to handle Option that way

I just encountered a need for custom validators to take &Option<T>. Use case is conditionally requiring a field to be defined if and only if another field is set to false (using custom validator args/context for this). If the other field is set to true then the optional field must be None.

That sounds like something that should be defined at the schema level. Or make it more like django forms and allow accessing other fields from any field validation function but that's a new feature.