Skip to main content

Validate

Trait Validate 

Source
pub trait Validate {
    // Required method
    fn validate(&self) -> Result<(), Box<ValidationErrors>>;
}
Expand description

Trait for types that can be validated.

Types implementing this trait can have their values checked against defined constraints, returning validation errors if any constraints are violated.

§Deriving

Use the #[derive(Validate)] macro from fastapi_macros to automatically implement this trait based on field attributes.

§Example

use fastapi_macros::Validate;
use fastapi_core::validation::Validate;

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

let user = CreateUser {
    email: "test@example.com".to_string(),
    username: "testuser".to_string(),
};

assert!(user.validate().is_ok());

Required Methods§

Source

fn validate(&self) -> Result<(), Box<ValidationErrors>>

Validate this value against all defined constraints.

§Errors

Returns ValidationErrors if any constraints are violated.

Implementors§