Skip to main content

Validate

Trait Validate 

Source
pub trait Validate {
    // Required method
    fn validate(&self) -> Result<(), ValidationErrors>;

    // Provided method
    fn validate_with_context(
        &self,
        _ctx: &dyn Any,
    ) -> Result<(), ValidationErrors> { ... }
}
Expand description

The core validation trait for Rusdantic.

This trait is automatically implemented by #[derive(Rusdantic)]. It validates all fields of a struct according to their declared rules and returns all validation errors at once (collect-all, not fail-fast).

§Example

use rusdantic::{Rusdantic, Validate};

#[derive(Rusdantic)]
struct User {
    #[rusdantic(email)]
    email: String,
}

let user = User { email: "not-an-email".to_string() };
assert!(user.validate().is_err());

Required Methods§

Source

fn validate(&self) -> Result<(), ValidationErrors>

Validate this value, collecting all validation errors.

Returns Ok(()) if all validation rules pass, or Err(ValidationErrors) containing all validation failures with their field paths.

Provided Methods§

Source

fn validate_with_context(&self, _ctx: &dyn Any) -> Result<(), ValidationErrors>

Validate with external context (database connection, config, etc.).

This method enables validators that need access to external resources. The default implementation ignores the context and delegates to validate().

§Example
struct DbContext { /* ... */ }

let user = User { email: "user@example.com".to_string() };
let ctx = DbContext { /* ... */ };
user.validate_with_context(&ctx)?;

Implementations on Foreign Types§

Source§

impl<T: Validate> Validate for Box<T>

Source§

impl<T: Validate> Validate for Rc<T>

Source§

impl<T: Validate> Validate for Arc<T>

Implementors§