pub trait Rule<T>: Send + Syncwhere
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§
Sourcefn validate(
&self,
value: &T,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>
fn validate( &self, value: &T, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>
Validate the value.
Sourcefn default_message(&self) -> String
fn default_message(&self) -> String
Get the default error message.
Provided Methods§
Sourcefn validate_at(
&self,
value: &T,
path: &FieldPath,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>
fn validate_at( &self, value: &T, path: &FieldPath, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>
Validate the value with a field path for error reporting.
Sourcefn error_code(&self) -> String
fn error_code(&self) -> String
Get the error code.
Sourcefn is_transform(&self) -> bool
fn is_transform(&self) -> bool
Check if this rule is a transformation (not validation).