Skip to main content

Rule

Trait Rule 

Source
pub trait Rule {
    // Required method
    fn check(&self, value: &Inspected) -> Result<(), String>;
}
Expand description

A rule to reuse across forms, e.g. a tax number:

use renox::validation::{Inspected, Rule};

struct TaxId;

impl Rule for TaxId {
    fn check(&self, value: &Inspected) -> std::result::Result<(), String> {
        let Inspected::Text(text) = value else { return Ok(()) };
        let digits = text.chars().filter(char::is_ascii_digit).count();
        if digits == 15 || digits == 16 {
            Ok(())
        } else {
            Err("The :attribute must be a valid tax ID.".into()) // :attribute is the field's label
        }
    }
}

fn rules(&self, v: &mut Validator) {
    v.field("tax_id", &self.tax_id).required().apply(&TaxId);
}

Missing values skip the rule (combine it with required).

Required Methods§

Source

fn check(&self, value: &Inspected) -> Result<(), String>

Err(message) when value breaks the rule.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§