You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I feel like it would be useful to have the ability to have const args for the validator.
Example scenario / use:
use validator::{Validate,ValidateArgs,ValidationError};fnvec_string_max_len(value:&Vec<String>,const_arg:u64) -> Result<(),ValidationError>{for s in value.iter(){if s.length() > arg {returnErr(ValidationError::new("String too long"));}}Ok(())}#[derive(Debug,Validate)]structTestStruct{#[validate(length(max = 10), custom(function = "vec_string_max_len", const_arg = "64"))]many_strings:Vec<String>,}let test_struct:TestStruct = [...]test_struct.validate().is_ok();// many_strings contain at most 10 strings, and each string is at most 64 characters long
That way, such validation can be defined in the model and don't need to be passed around each time you validate.
As of now, unless I am missing something, I don't really see a way to make such a validator in a way which doesn't require either passing along data in validate_args(...) or writing a validate function specific to that field only.
The suggested syntax above is to mirror the syntax of the current validate-time args, I don't really like it but that's a start 😅
The text was updated successfully, but these errors were encountered:
Samuel-B-D
changed the title
Support for litteral args in custom validators
Support for const args in custom validators
Jul 6, 2022
I feel like it would be useful to have the ability to have const args for the validator.
Example scenario / use:
That way, such validation can be defined in the model and don't need to be passed around each time you validate.
As of now, unless I am missing something, I don't really see a way to make such a validator in a way which doesn't require either passing along data in
validate_args(...)
or writing a validate function specific to that field only.The suggested syntax above is to mirror the syntax of the current validate-time args, I don't really like it but that's a start 😅
The text was updated successfully, but these errors were encountered: