Keats / validator

Simple validation for Rust structs
MIT License
1.91k stars 140 forks source link

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

Closed williamlsh closed 3 months ago

williamlsh commented 4 months ago

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>.

Keats commented 4 months ago

I thought the previous behaviour was a bug (https://github.com/Keats/validator/issues/303) but I guess it makes more sense to handle Option that way

chris13524 commented 4 months ago

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.

Keats commented 4 months ago

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.