#[derive(Validify)]
{
// Attributes available to this derive:
#[modify]
#[validate]
#[validify]
}
Expand description
Implements Validate and Modify in one trait.
Deriving this will allow you to annotate fields with modify attributes, in addition to validate.
Modifiers are simple functions that modify the struct before validation.
You can use the few out of the box ones or create your own.
Visit the repository to see the list of available validations and modifiers, as well as more examples.
§Example
ⓘ
use validify::{Validify, Payload};
#[derive(Debug, Clone, serde::Deserialize, Validify, Payload)]
struct Testor {
#[modify(lowercase, trim)]
#[validate(length(equal = 8))]
pub a: String,
#[modify(trim, uppercase)]
pub b: Option<String>,
#[modify(custom(do_something))]
pub c: String,
#[modify(custom(do_something))]
pub d: Option<String>,
#[validify]
pub nested: Nestor,
}
#[derive(Debug, Clone, serde::Deserialize, Validify, Payload)]
struct Nestor {
#[modify(trim, uppercase)]
#[validate(length(equal = 12))]
a: String,
#[modify(capitalize)]
#[validate(length(equal = 14))]
b: String,
}
fn do_something(input: &mut String) {
*input = String::from("modified");
}
let mut test = Testor {
a: " LOWER ME ".to_string(),
b: Some(" makemeshout ".to_string()),
c: "I'll never be the same".to_string(),
d: Some("Me neither".to_string()),
nested: Nestor {
a: " notsotinynow ".to_string(),
b: "capitalize me.".to_string(),
},
};
// The magic line
let res = test.validify();
assert!(matches!(res, Ok(_)));
// Parent
assert_eq!(test.a, "lower me");
assert_eq!(test.b, Some("MAKEMESHOUT".to_string()));
assert_eq!(test.c, "modified");
assert_eq!(test.d, Some("modified".to_string()));
// Nested
assert_eq!(test.nested.a, "NOTSOTINYNOW");
assert_eq!(test.nested.b, "Capitalize me.");