Rule

Trait Rule 

Source
pub trait Rule<T>: Send + Sync
where T: ?Sized,
{ // Required methods fn validate( &self, value: &T, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>; fn name(&self) -> &'static str; fn default_message(&self) -> String; // Provided methods fn validate_at( &self, value: &T, path: &FieldPath, ctx: &ValidationContext, ) -> Result<(), ValidationErrors> { ... } fn error_code(&self) -> String { ... } fn is_transform(&self) -> bool { ... } }
Expand description

Trait for individual validation rules.

Each validation rule (email, length, range, etc.) implements this trait. Rules are generic over the value type they validate.

§Example

use skp_validator_core::{Rule, ValidationContext, ValidationResult};

struct MinLengthRule {
    min: usize,
}

impl Rule<str> for MinLengthRule {
    fn validate(&self, value: &str, _ctx: &ValidationContext) -> ValidationResult<()> {
        if value.len() >= self.min {
            Ok(())
        } else {
            Err(ValidationErrors::from_error(
                ValidationError::new("", "length.min", "Too short")
            ))
        }
    }

    fn name(&self) -> &'static str { "min_length" }
    fn default_message(&self) -> String { format!("Must be at least {} characters", self.min) }
}

Required Methods§

Source

fn validate( &self, value: &T, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Validate the value.

Source

fn name(&self) -> &'static str

Get the rule name for error reporting.

Source

fn default_message(&self) -> String

Get the default error message.

Provided Methods§

Source

fn validate_at( &self, value: &T, path: &FieldPath, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Validate the value with a field path for error reporting.

Source

fn error_code(&self) -> String

Get the error code.

Source

fn is_transform(&self) -> bool

Check if this rule is a transformation (not validation).

Implementations on Foreign Types§

Source§

impl Rule<f32> for MultipleOfRule<f64>

Source§

fn validate( &self, value: &f32, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<f32> for RangeRule<f32>

Source§

fn validate( &self, value: &f32, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<f64> for AllowedValuesRule<f64>

Source§

fn validate( &self, value: &f64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<f64> for MultipleOfRule<f64>

Source§

fn validate( &self, value: &f64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<f64> for RangeRule<f64>

Source§

fn validate( &self, value: &f64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i8> for RangeRule<i8>

Source§

fn validate( &self, value: &i8, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i16> for RangeRule<i16>

Source§

fn validate( &self, value: &i16, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i32> for AllowedValuesRule<i32>

Source§

fn validate( &self, value: &i32, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i32> for MultipleOfRule<i64>

Source§

fn validate( &self, value: &i32, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i32> for RangeRule<i32>

Source§

fn validate( &self, value: &i32, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i64> for AllowedValuesRule<i64>

Source§

fn validate( &self, value: &i64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i64> for MultipleOfRule<i64>

Source§

fn validate( &self, value: &i64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i64> for RangeRule<i64>

Source§

fn validate( &self, value: &i64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<i128> for RangeRule<i128>

Source§

fn validate( &self, value: &i128, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<isize> for RangeRule<isize>

Source§

fn validate( &self, value: &isize, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for AllowedValuesRule<&str>

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for AllowedValuesRule<String>

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for MustMatchRule

Source§

fn validate( &self, value: &str, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for RequiredRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for DependencyRule<str>

Source§

fn validate( &self, value: &str, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for CreditCardRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for AlphanumericRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for AsciiRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for ContainsRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for PrefixRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for SuffixRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<str> for LengthRule

Source§

fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u8> for RangeRule<u8>

Source§

fn validate( &self, value: &u8, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u16> for RangeRule<u16>

Source§

fn validate( &self, value: &u16, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u32> for AllowedValuesRule<u32>

Source§

fn validate( &self, value: &u32, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u32> for RangeRule<u32>

Source§

fn validate( &self, value: &u32, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u64> for AllowedValuesRule<u64>

Source§

fn validate( &self, value: &u64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u64> for RangeRule<u64>

Source§

fn validate( &self, value: &u64, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<u128> for RangeRule<u128>

Source§

fn validate( &self, value: &u128, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<usize> for RangeRule<usize>

Source§

fn validate( &self, value: &usize, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for AllowedValuesRule<String>

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for MustMatchRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for RequiredRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for DependencyRule<String>

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for CreditCardRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for AlphanumericRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for AsciiRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for ContainsRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for PrefixRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for SuffixRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl Rule<String> for LengthRule

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T> Rule<Option<T>> for RequiredRule

Source§

fn validate( &self, value: &Option<T>, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T> Rule<[T]> for UniqueItemsRule
where T: Eq + Hash,

Source§

fn validate( &self, value: &[T], _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T> Rule<Vec<T>> for UniqueItemsRule
where T: Eq + Hash,

Source§

fn validate( &self, value: &Vec<T>, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T> Rule<Vec<T>> for RequiredRule

Source§

fn validate( &self, value: &Vec<T>, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T> Rule<Vec<T>> for LengthRule

Source§

fn validate( &self, value: &Vec<T>, _ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T> Rule<T> for ContextualRule<T>
where T: 'static + ?Sized,

Source§

fn validate( &self, value: &T, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T, F> Rule<T> for CustomFnRule<T, F>
where F: Fn(&T, &ValidationContext) -> bool + Send + Sync, T: ?Sized,

Source§

fn validate( &self, value: &T, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Source§

impl<T, F> Rule<T> for CustomResultRule<T, F>

Source§

fn validate( &self, value: &T, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>

Source§

fn name(&self) -> &'static str

Source§

fn default_message(&self) -> String

Implementors§

Source§

impl<T> Rule<T> for AnyRule<T>
where T: Sync + ?Sized,

Source§

impl<T> Rule<T> for CompositeRule<T>
where T: Sync + ?Sized,