pub trait ValidationRule<T: ?Sized>:
Debug
+ Send
+ Sync {
// Required methods
fn validate(&self, value: &T) -> Result<(), RuleError>;
fn rule_name(&self) -> &'static str;
// Provided method
fn default_message(&self) -> String { ... }
}Expand description
Trait for individual validation rules.
Each rule validates a single value and returns a RuleError on failure.
Rules should be serializable for configuration and pretty-printing.
§Example
ⓘ
use rustapi_validate::v2::prelude::*;
struct PositiveRule;
impl ValidationRule<i32> for PositiveRule {
fn validate(&self, value: &i32) -> Result<(), RuleError> {
if *value > 0 {
Ok(())
} else {
Err(RuleError::new("positive", "Value must be positive"))
}
}
fn rule_name(&self) -> &'static str {
"positive"
}
}Required Methods§
Provided Methods§
Sourcefn default_message(&self) -> String
fn default_message(&self) -> String
Get the default error message for this rule.