Macro validate

Source
macro_rules! validate {
    ($vec:expr, $test:expr, $field_name:expr, $message:expr) => { ... };
}
Expand description

A macro to simplify validation checks.

Usage: validate!(vec, (boolean test expression), "field", "message")

This macro checks the provided boolean test expression, and if it evaluates to true, it pushes a new ValidationError onto the provided vector.

§Arguments

  • $vec - The vector to which the ValidationError will be added.
  • $test - A boolean expression that determines whether to add the error.
  • $field_name - The name of the field related to the validation error.
  • $message - A message describing the validation error.

§Examples

use struct_validation_core::{validate, ValidationError};

let mut errors = Vec::new();
let username = "";
validate!(errors, username.is_empty(), "username", "must not be empty");
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].field, "username");
assert_eq!(errors[0].message, "must not be empty");