[][src]Derive Macro vale::Validate

#[derive(Validate)]
{
    // Attributes available to this derive:
    #[validate]
}

A proc macro used to implement Validate automatically for a struct.

There are a couple of options for validating a structure. The are listed below:

  • lt: Check if the value is less than the provided argument,
  • eq: check if the value is equal to the provided argument,
  • gt: check if the value is greater than the provided argument,
  • neq: check if the len() of the value is not equal to the provided argument,
  • len_lt: Check if the len() of the value is less than the provided argument,
  • len_eq: check if the len() of the value is equal to the provided argument,
  • len_gt: check if the len() of the value is greater than the provided argument,
  • len_neq: check if the len() of the value is not equal to the provided argument,
  • with: Rrn the provided function to perform validation,
  • trim: always succeeds, and trims the string that is inputted,
  • to_lower_case: convert the provided value to lowercase.

Example

#[derive(vale::Validate)]
struct Struct {
    #[validate(gt(10))]
    value: u32,
    #[validate(len_gt(3))]
    string: String,
    #[validate(eq(true))]
    boolean: bool,
    #[validate(with(is_even))]
    even_value: i32,
    #[validate(trim, len_lt(10), to_lower_case)]
    transformer: String,
    #[validate(len_lt(10), trim)]
    transfailer: String,
}

fn is_even(num: &mut i32) -> bool {
    *num % 2 == 0
}