Macro validus::easy_rule

source ·
macro_rules! easy_rule {
    ($name:ident, err = $err:ty, $func:expr) => { ... };
}
Expand description

Define a rule using a closure.

The closure that returns a Result is the validation function.

It will be used to implement ValidateString for the rule.

See Also

Example

use validus::vstr::{vstr, ValidateString, StrExt};
use validus::easy_rule;

// Declare my rule
struct MyRule;

// Implement `ValidateString` for my rule.
easy_rule!(MyRule, err = &'static str, |s: &str| {
    (s.len() > 5).then(|| ()).ok_or("string is too short")
});

// `StrExt` allows you to call `.validate` on any `str`-slice.
let vv: &vstr<MyRule> = "hello world".validate::<MyRule>().unwrap();
assert_eq!(vv, "hello world");