modo/validate/traits.rs
1use super::ValidationError;
2
3/// Types that can validate their own fields.
4///
5/// Implement this trait on request structs and call `validate()` inside your
6/// handler before processing the input. Use `?` to propagate the
7/// [`ValidationError`] as a [`crate::Error`] (HTTP 422).
8///
9/// # Example
10///
11/// ```rust,no_run
12/// use modo::validate::{Validate, ValidationError, Validator};
13///
14/// struct SignUp {
15/// username: String,
16/// }
17///
18/// impl Validate for SignUp {
19/// fn validate(&self) -> Result<(), ValidationError> {
20/// Validator::new()
21/// .field("username", &self.username, |f| f.required().min_length(3))
22/// .check()
23/// }
24/// }
25/// ```
26pub trait Validate {
27 /// Validate this value, returning all field-level errors at once.
28 ///
29 /// # Errors
30 ///
31 /// Returns [`ValidationError`] containing every failing field and its
32 /// error messages when one or more fields are invalid.
33 fn validate(&self) -> Result<(), ValidationError>;
34}