Validate

Trait Validate 

Source
pub trait Validate: Validate {
    // Provided methods
    fn validate(&self) -> Result<(), ValidationError> { ... }
    fn validated(self) -> Result<Self, ValidationError>
       where Self: Sized { ... }
}
Expand description

Trait for validatable types.

This trait wraps the validator::Validate trait and provides a RustAPI-native interface for validation.

§Example

use rustapi_validate::prelude::*;
use validator::Validate as ValidatorValidate;

#[derive(ValidatorValidate)]
struct CreateUser {
    #[validate(email)]
    email: String,
     
    #[validate(length(min = 3, max = 50))]
    username: String,
}

impl Validate for CreateUser {}

fn example() {
    let user = CreateUser {
        email: "invalid".to_string(),
        username: "ab".to_string(),
    };

    match user.validate() {
        Ok(()) => println!("Valid!"),
        Err(e) => println!("Errors: {:?}", e.fields),
    }
}

Provided Methods§

Source

fn validate(&self) -> Result<(), ValidationError>

Validate the struct and return a ValidationError on failure.

Source

fn validated(self) -> Result<Self, ValidationError>
where Self: Sized,

Validate and return the struct if valid, error otherwise.

Implementors§

Source§

impl<T: Validate> Validate for T