Skip to main content

modo/validate/
mod.rs

1//! # modo::validate
2//!
3//! Input validation for request data.
4//!
5//! Provides:
6//! - [`Validator`] — fluent builder that collects per-field validation errors
7//! - [`ValidationError`] — per-field error collection; converts into [`crate::Error`] (HTTP 422)
8//! - [`Validate`] — trait for types that validate themselves
9//!
10//! Field rules are applied through a `FieldValidator` obtained inside the
11//! [`Validator::field`] closure. String rules require `T: AsRef<str>`; numeric
12//! rules require `T: PartialOrd + Display`.
13//!
14//! [`ValidationError`] converts automatically into [`crate::Error`] via the
15//! `From` impl, producing an HTTP 422 Unprocessable Entity response whose
16//! `details` field contains the per-field error map.
17//!
18//! All three public items ([`Validate`], [`ValidationError`], [`Validator`])
19//! are re-exported from [`crate::prelude`].
20//!
21//! ## Quick start
22//!
23//! ```rust,no_run
24//! use modo::validate::{Validate, ValidationError, Validator};
25//!
26//! struct CreateUser {
27//!     name: String,
28//!     email: String,
29//!     age: u32,
30//! }
31//!
32//! impl Validate for CreateUser {
33//!     fn validate(&self) -> Result<(), ValidationError> {
34//!         Validator::new()
35//!             .field("name", &self.name, |f| {
36//!                 f.required().min_length(2).max_length(100)
37//!             })
38//!             .field("email", &self.email, |f| f.required().email())
39//!             .field("age", &self.age, |f| f.range(18..=120))
40//!             .check()
41//!     }
42//! }
43//! ```
44
45mod error;
46mod rules;
47mod traits;
48mod validator;
49
50pub use error::ValidationError;
51pub use traits::Validate;
52pub use validator::Validator;