Expand description
§Example:
ⓘ
use serde::Deserialize;
// A trait that the Validate derive will impl
use validator::{Validate, ValidationError};
#[derive(Debug, Validate, Deserialize)]
struct SignupData {
    #[validate(email)]
    mail: String,
    #[validate(url)]
    site: String,
    #[validate(length(min = 1), custom = "validate_unique_username")]
    #[serde(rename = "firstName")]
    first_name: String,
    #[validate(range(min = 18, max = 20))]
    age: u32,
}
fn validate_unique_username(username: &str) -> Result<(), ValidationError> {
    if username == "xXxShad0wxXx" {
        // the value of the username will automatically be added later
        return Err(ValidationError::new("terrible_username"));
    }
    Ok(())
}
match signup_data.validate() {
  Ok(_) => (),
  Err(e) => return e;
};§Available Validations:
| Validation | Notes | 
|---|---|
| email | |
| url | |
| length | |
| range | |
| must_match | |
| contains | |
| does_not_contain | |
| custom | |
| regex | |
| credit_card | (Requires the feature cardto be enabled) | 
| non_control_character | (Required the feature unicto be enabled) | 
| nested | (Uses the validation of the field type it self) | 
| required | 
Checkout the project README of an in-depth usage description with examples.
§Installation:
Add the validator to the dependencies in the Cargo.toml file.
[dependencies]
validator = { version = "0.16", features = ["derive"] }Structs§
Enums§
Traits§
- Validate
- This is the original trait that was implemented by deriving Validate. It will still be implemented for struct validations that don’t take custom arguments. The call is being forwarded to theValidateArgs<'v_a>trait.
- ValidateArgs 
- This trait will be implemented by deriving Validate. This implementation can take one argument and pass this on to custom validators. The defaultArgstype will be()if there is no custom validation with defined arguments.
- ValidateContains 
- ValidateDoes NotContain 
- ValidateEmail 
- 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.
- ValidateIp 
- ValidateLength 
- Validates the length of the value given.
If the validator has equalset, it will ignore anyminandmaxvalue.
- ValidateNested 
- ValidateRange 
- Validates that the given valueis inside the defined range. Themax,min,exclusive_maxandexclusive_minparameters are optional and will only be validated if they are notNone
- ValidateRegex 
- ValidateRequired 
- Validates whether the given Option is Some
- ValidateUrl 
- Validates whether the string given is a url
Functions§
- validate_must_ match 
- Validates that the 2 given fields match. Both fields are optionals