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§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".