pub trait Validate {
// Required method
fn validate_with_context(
&self,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>;
// Provided methods
fn validate(&self) -> Result<(), ValidationErrors> { ... }
fn validate_and_transform(&self) -> Result<Self, ValidationErrors>
where Self: Sized + Clone { ... }
}Expand description
Core validation trait for types that can be validated.
This trait is typically implemented by the derive macro, but can also be implemented manually for custom validation logic.
§Example
ⓘ
use skp_validator_core::{Validate, ValidationContext, ValidationResult};
struct User {
name: String,
email: String,
}
impl Validate for User {
fn validate_with_context(&self, ctx: &ValidationContext) -> ValidationResult<()> {
let mut errors = ValidationErrors::new();
if self.name.is_empty() {
errors.add_field_error("name",
ValidationError::new("name", "required", "Name is required"));
}
if errors.is_empty() { Ok(()) } else { Err(errors) }
}
}Required Methods§
Sourcefn validate_with_context(
&self,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>
fn validate_with_context( &self, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>
Validate this instance with a custom context.
This is the primary validation method that should be implemented.
Provided Methods§
Sourcefn validate(&self) -> Result<(), ValidationErrors>
fn validate(&self) -> Result<(), ValidationErrors>
Validate this instance with default context.
Sourcefn validate_and_transform(&self) -> Result<Self, ValidationErrors>
fn validate_and_transform(&self) -> Result<Self, ValidationErrors>
Validate and return a transformed copy (if transformations are applied).
This method is useful when validation includes transformations like trimming, case conversion, etc.