pub trait Rule: Send + Sync {
// Required methods
fn id(&self) -> &str;
fn validate(&self, value: &str, path: &str) -> Vec<ValidationError>;
}Expand description
A validation rule that can be applied to a string value at a given path.
Implement this trait to create custom validation rules.
§Examples
use mx20022_validate::rules::Rule;
use mx20022_validate::error::{ValidationError, Severity};
struct NonEmptyRule;
impl Rule for NonEmptyRule {
fn id(&self) -> &'static str { "NON_EMPTY" }
fn validate(&self, value: &str, path: &str) -> Vec<ValidationError> {
if value.is_empty() {
vec![ValidationError::new(path, Severity::Error, "NON_EMPTY", "Value must not be empty")]
} else {
vec![]
}
}
}