Crate validify

source ·
Expand description

Validify is a procedural macro aimed to provide quick data validation and modification implementations. Its primary use case is aimed towards web payloads.

The traits it exposes are Validify and Validate.

Deriving Validify will allow you to modify structs before they are validated by providing a few out of the box implementations as well as the ability to write custom ones. It will also generate a payload struct for the deriving struct, which can be used in the context of web payloads. The payload struct is just a copy of the original, except will all the fields being Options. This enables the payload to be fully deserialized (given that all existing fields are of the correct type) before being validated to allow for better validation errors.

Deriving Validate will allow you to specify struct validations, but does not create an associated payload struct. Validate can be derived on structs containing references, while Validify cannot due to modifiers.

Visit the repository to see the list of available validations and modifiers as well as more examples.

Example

use validify::Validify;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Validify)]
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,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Validify)]
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.");

Modules

Macros

Structs

Enums

Traits

  • Modifies the 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 is not intended to be implemented manually. It should be derived with the #[derive(Validify)] attribute which automatically implements Validate, Modify and creates the payload struct.

Functions

Attribute Macros

  • A shortcut for ergonomic error creation in custom schema validator functions.

Derive Macros

  • Derives Validate based on the provided field attributes.
  • Combines Validate and Modify in one trait and provides the intermediary payload struct.