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
Calls one of your functions to perform a custom validation. The field reference will be given as a parameter to the function, which should return a Result<(), ValidationError>.
However, as far as I tested:
For String field, a custom function receives &String, which is as expected.
However, for usize field, a custom function receives usize instead of &usize, contradicting with the snippet above.
Minimal Working Example
use validator::{Validate,ValidationError};//compile error (changing `&usize` to `usize` fixes the error)fnvalidate_usize(_:&usize) -> Result<(),ValidationError>{Ok(())}//OK (inconsistent with the function above)fnvalidate_string(_:&str) -> Result<(),ValidationError>{Ok(())}#[derive(Debug,Validate)]structS{#[validate(custom(function = "validate_usize"))]i:usize,#[validate(custom(function = "validate_string"))]j:String,}fnmain(){let s = S{i:10,j:"hello".to_string(),};assert!(s.validate().is_ok());}
Compiling a v0.1.0 (/Users/user/a)
error[E0308]: mismatched types
--> src/main.rs:13:17
|
13 | #[derive(Debug, Validate)]
| ^^^^^^^^ expected `&usize`, found `usize`
14 | struct S {
15 | #[validate(custom(function = "validate_usize"))]
| ---------------- arguments to this function are incorrect
|
note: function defined here
--> src/main.rs:4:4
|
4 | fn validate_usize(_: &usize) -> Result<(), ValidationError> {
| ^^^^^^^^^^^^^^ ---------
= note: this error originates in the derive macro `Validate` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0308`.
error: could not compile `a` (bin "a") due to 1 previous error
The text was updated successfully, but these errors were encountered:
According to
README.md
(emphasis mine):However, as far as I tested:
For
String
field, a custom function receives&String
, which is as expected.However, for
usize
field, a custom function receivesusize
instead of&usize
, contradicting with the snippet above.Minimal Working Example
The text was updated successfully, but these errors were encountered: