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§
Sourcefn validate(&self) -> Result<(), ValidationErrors>
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§
Sourcefn validate_with_context(&self, _ctx: &dyn Any) -> Result<(), ValidationErrors>
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)?;