Skip to main content

Validate

Derive Macro Validate 

Source
#[derive(Validate)]
{
    // Attributes available to this derive:
    #[validate]
}
Available on crate feature macros only.
Expand description

Derive Validate for a named-field struct.

Annotate fields with #[validate(...)] rules. All failures are collected before returning, so the caller sees every invalid field in one response.

§Supported validators

SyntaxChecks
length(min = N)field.chars().count() >= N
length(max = N)field.chars().count() <= N
length(min = N, max = N)both bounds
range(min = N)field as f64 >= N
range(max = N)field as f64 <= N
range(min = N, max = N)both bounds
emaillocal part, @, domain with .
required!field.is_empty()
urlstarts with http:// or https://

§Example

ⓘ
#[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,
}