Skip to main content

Module validate

Module validate 

Source
Expand description

§modo::validate

Input validation for request data.

Always available (no feature flag required).

Provides:

Field rules are applied through a FieldValidator obtained inside the Validator::field closure. String rules require T: AsRef<str>; numeric rules require T: PartialOrd + Display.

ValidationError converts automatically into crate::Error via the From impl, producing an HTTP 422 Unprocessable Entity response whose details field contains the per-field error map.

All three types are re-exported at the crate root as crate::Validate, crate::ValidationError, and crate::Validator.

§Quick start

use modo::validate::{Validate, ValidationError, Validator};

struct CreateUser {
    name: String,
    email: String,
    age: u32,
}

impl Validate for CreateUser {
    fn validate(&self) -> Result<(), ValidationError> {
        Validator::new()
            .field("name", &self.name, |f| {
                f.required().min_length(2).max_length(100)
            })
            .field("email", &self.email, |f| f.required().email())
            .field("age", &self.age, |f| f.range(18..=120))
            .check()
    }
}

Structs§

ValidationError
A collection of per-field validation errors.
Validator
A builder that collects validation errors across multiple fields.

Traits§

Validate
Types that can validate their own fields.