Skip to main content

Rule

Trait Rule 

Source
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![]
        }
    }
}

Required Methods§

Source

fn id(&self) -> &str

Unique identifier for this rule (e.g. "IBAN_CHECK").

Source

fn validate(&self, value: &str, path: &str) -> Vec<ValidationError>

Run the rule against value at the given path and return any findings.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§