Skip to main content

Validate

Trait Validate 

Source
pub trait Validate<T>:
    Send
    + Sync
    + 'static {
    // Required method
    fn validate(body: &T) -> Result<(), String>;
}
Expand description

Trait for request body validators.

Implement this to define validation rules for a request body type. The validator runs after JSON deserialization but before the handler.

§Example

struct CreateUserValidator;

impl Validate<CreateUser> for CreateUserValidator {
    fn validate(body: &CreateUser) -> Result<(), String> {
        if body.username.is_empty() { return Err("username required".into()); }
        if body.password.len() < 8 { return Err("password too short".into()); }
        Ok(())
    }
}

type API = (
    Validated<CreateUserValidator, PostEndpoint<UsersPath, CreateUser, User>>,
);

Required Methods§

Source

fn validate(body: &T) -> Result<(), String>

Validate the deserialized body. Returns Err(message) on failure.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§