Skip to main content

ferro_rs/validation/
validatable.rs

1//! Validatable trait for derive-based validation.
2//!
3//! Structs implementing this trait can be validated using their field attributes.
4
5use crate::validation::{Rule, ValidationError};
6
7/// Trait for types that can validate themselves using declarative rules.
8///
9/// This trait is typically derived using `#[derive(Validate)]`:
10///
11/// ```rust,ignore
12/// use ferro_rs::Validate;
13///
14/// #[derive(Validate)]
15/// struct CreateUserRequest {
16///     #[validate(required, email)]
17///     email: String,
18///
19///     #[validate(required, min(8))]
20///     password: String,
21/// }
22///
23/// // Usage
24/// let request = CreateUserRequest { ... };
25/// request.validate()?;
26/// ```
27pub trait Validatable {
28    /// Validate the struct against its declared rules.
29    ///
30    /// Returns `Ok(())` if validation passes, or `Err(ValidationError)` with
31    /// all validation errors collected.
32    fn validate(&self) -> Result<(), ValidationError>;
33
34    /// Get the static rule definitions for this type.
35    ///
36    /// Returns a list of (field_name, rules) tuples that can be used
37    /// for introspection (e.g., by ferro-mcp).
38    fn validation_rules() -> Vec<(&'static str, Vec<Box<dyn Rule>>)>;
39}