Crate validify

source ·
Expand description

A procedural macro built on top of the validator crate that provides attributes for field modifiers. Particularly useful in the context of web payloads.

Visit the repository to see exactly how it works.

Example

use validify::{validify, Validify};

#[validify]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct Testor {
    #[modify(lowercase, trim)]
    #[validate(length(equal = 8))]
    pub a: String,
    #[modify(trim, uppercase)]
    pub b: Option<String>,
    #[modify(custom = "do_something")]
    pub c: String,
    #[modify(custom = "do_something")]
    pub d: Option<String>,
    #[validify]
    pub nested: Nestor,
}

#[validify]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct Nestor {
    #[modify(trim, uppercase)]
    #[validate(length(equal = 12))]
    a: String,
    #[modify(capitalize)]
    #[validate(length(equal = 14))]
    b: String,
}

fn do_something(input: &mut String) {
    *input = String::from("modified");
}

let mut test = Testor {
  a: "   LOWER ME     ".to_string(),
  b: Some("  makemeshout   ".to_string()),
  c: "I'll never be the same".to_string(),
  d: Some("Me neither".to_string()),
  nested: Nestor {
    a: "   notsotinynow   ".to_string(),
      b: "capitalize me.".to_string(),
  },
};

// The magic line
let res = Testor::validify(test.into());

assert!(matches!(res, Ok(_)));

let test = res.unwrap();

// Parent
assert_eq!(test.a, "lower me");
assert_eq!(test.b, Some("MAKEMESHOUT".to_string()));
assert_eq!(test.c, "modified");
assert_eq!(test.d, Some("modified".to_string()));
// Nested
assert_eq!(test.nested.a, "NOTSOTINYNOW");
assert_eq!(test.nested.b, "Capitalize me.");

Macros

Designed to be used with the schema_validation proc macro. Used for ergonomic custom error handling.
Designed to be used with the schema_validation proc macro. Used for ergonomic custom error handling.

Structs

Enums

Traits

Modifies struct based on the provided modify parameters. Automatically implemented when deriving Validify.
Validates structs based on the provided validate parameters. Can be implemented on its own if one doesn’t need payload modifications.
Combines Validate and Modify in one trait and provides the intermediary payload struct. This trait should never be implemented manually, and should be derived with the #[validify] macro which automatically implements Validate, Modify and Validify.

Functions

Validates whether the value contains the needle The value needs to implement the Contains trait, which is implement on String, str and Hashmap by default.
Validates whether the value does not contain the needle The value needs to implement the Contains trait, which is implement on String, str and Hashmap by default.
Validates whether the given string is an email based on the HTML5 spec. RFC 5322 is not practical in most circumstances and allows email addresses that are unfamiliar to most users.
Validates whether the given string is an IP
Validates whether the given string is an IP V4
Validates whether the given string is an IP V6
Validates the length of the value given. If the validator has equal set, it will ignore any min and max value.
Validates that the 2 given fields match. Both fields are optionals
Validates that the given value is inside the defined range. The max and min parameters are optional and will only be validated if they are not None
Validates whether the given Option is Some
Validates whether the string given is a url

Attribute Macros

A shortcut for ergonomic error creation in custom schema validator functions.
Shortcut for deriving both the Validate and Validify traits in a one liner.

Derive Macros