Expand description
Validify is a procedural macro aimed to provide quick data validation and modification implementations. Its primary use case is aimed towards web payloads.
The traits it exposes are Validify and Validate.
Deriving Validify will allow you to modify structs before they are validated by providing a few out of the box implementations
as well as the ability to write custom ones. It will also generate a payload struct for the deriving struct,
which can be used in the context of web payloads. The payload struct is just a copy of the original, except will all the fields being
Options. This enables the payload to be fully deserialized (given that all existing fields are of the correct type) before being validated
to allow for better validation errors.
Deriving Validate will allow you to specify struct validations, but does not create an associated payload struct. Validate can be derived on structs containing references, while Validify cannot due to modifiers.
Visit the repository to see the list of available validations and modifiers as well as more examples.
Example
use validify::Validify;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Validify)]
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::Serialize, serde::Deserialize, Validify)]
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 = Testor::validify(test.into());
assert!(matches!(res, Ok(_)));
let test = res.unwrap();
// 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.");Modules
Macros
- Designed to be used with the schema_validation proc macro. Used for ergonomic custom error handling.
- Designed to be used with the schema_validation proc macro. Used for ergonomic custom error handling.
Structs
Enums
Traits
- Modifies the struct based on the provided
modifyparameters. Automatically implemented when deriving Validify. - Validates structs based on the provided
validateparameters. Can be implemented on its own if one doesn’t need payload modifications. - Combines
ValidateandModifyin one trait and provides the intermediary payload struct. This trait is not intended to be implemented manually. It should be derived with the#[derive(Validify)]attribute which automatically implementsValidate,Modifyand creates the payload struct.
Functions
- Validates whether the value contains the needle The value needs to implement the Contains trait, which is implemented on String, str and
Hashmap<String>by default. - Validates whether the given string is an email based on the HTML5 spec. RFC 5322 is not practical in most circumstances and allows email addresses that are unfamiliar to most users.
- Validates whether or not a slice contains an element
- Validates whether the given string is an IP
- Validates whether the given string is an IP V4
- Validates whether the given string is an IP V6
- Validates the length of the value given. If the validator has
equalset, it will ignore anyminandmaxvalue. - Validates that the 2 given fields match. Both fields are optionals
- Validates that the given
valueis inside the defined range. Themaxandminparameters are optional and will only be validated if they are notNone - Validates whether the given Option is Some
- Validates whether the string given is a url
Attribute Macros
- A shortcut for ergonomic error creation in custom schema validator functions.
Derive Macros
- Derives
Validatebased on the provided field attributes. - Combines
ValidateandModifyin one trait and provides the intermediary payload struct.