pub trait Validate {
// Required method
fn validate(&self) -> Result<(), ValidationErrors>;
}Expand description
Types that can be validated field-by-field.
Implement manually or derive with #[derive(Validate)]
(requires features = ["macros"]).
§Example — manual implementation
use rust_web_server::validate::{Validate, ValidationErrors};
struct Payload { name: String }
impl Validate for Payload {
fn validate(&self) -> Result<(), ValidationErrors> {
let mut errors = ValidationErrors::new();
if self.name.is_empty() { errors.add("name", "must not be empty"); }
if errors.is_empty() { Ok(()) } else { Err(errors) }
}
}§Example — derive macro
ⓘ
use rust_web_server::validate::Validate;
#[derive(rust_web_server::Validate)]
struct CreateUser {
#[validate(length(min = 1, max = 50))]
name: String,
#[validate(email)]
email: String,
#[validate(range(min = 0, max = 150))]
age: u8,
}Supported validators: email, required, url,
length(min = N, max = N), range(min = N, max = N).
Required Methods§
fn validate(&self) -> Result<(), ValidationErrors>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".