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