validate

Macro validate 

Source
macro_rules! validate {
    ($($key:ident = $val:expr),+ $(,)?) => { ... };
}
Expand description

Combines multiple Validation results into a single one.

This macro allows you to validate multiple fields in parallel and accumulate all errors if any occur. If all validations succeed, it returns a tuple containing the successful values.

§Syntax

validate!(
    field1 = validation_expr1,
    field2 = validation_expr2,
    ...
)

§Returns

Validation<E, (T1, T2, ...)> where T1, T2 are the success types of the expressions.

§Examples

use error_rail::{validate, validation::Validation};

let v1 = Validation::<&str, i32>::valid(1);
let v2 = Validation::<&str, i32>::valid(2);

let result = validate!(
    a = v1,
    b = v2
);

assert!(result.is_valid());
assert_eq!(result.into_value(), Some((1, 2)));

let e1 = Validation::<&str, i32>::invalid("error1");
let e2 = Validation::<&str, i32>::invalid("error2");

let result = validate!(
    a = e1,
    b = e2
);

assert!(result.is_invalid());
assert_eq!(result.into_errors().unwrap().len(), 2);