Validate

Trait Validate 

Source
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§

Source

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§

Source

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

Validate this instance with default context.

Source

fn validate_and_transform(&self) -> Result<Self, ValidationErrors>
where Self: Sized + Clone,

Validate and return a transformed copy (if transformations are applied).

This method is useful when validation includes transformations like trimming, case conversion, etc.

Implementations on Foreign Types§

Source§

impl<T> Validate for &T
where T: Validate + ?Sized,

Source§

impl<T> Validate for Box<T>
where T: Validate + ?Sized,

Source§

impl<T> Validate for Rc<T>
where T: Validate + ?Sized,

Source§

impl<T> Validate for Arc<T>
where T: Validate + ?Sized,

Implementors§