vale-derive 0.0.0

Macros for vale
Documentation
  • Coverage
  • 0%
    0 out of 4 items documented0 out of 3 items with examples
  • Size
  • Source code size: 24.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 185.1 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ThouCheese/vale
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ThouCheese

Vale

vale on crates.io stripe-rust on docs.rs

Vale stands for Valid Entity, and is a simple library that provides entity validation through either annotations, or through a Fluent-style implementation. At the core of the library is the vale::Validate trait, which implies that a piece of data can be validated. The library also offers supoort for the rocket webframework. If you're interested in adding support for other frameworks, do not hesitate to open a PR!

Example

This example shows how to derive the validation trait

use vale::Validate;

#[derive(Validate)]
struct Entity {
    #[validate(gt(0))]
    id: i32,
    #[validate(len_lt(5), with(add_element))]
    others: Vec<i32>,
    #[validate(eq("hello world"))]
    msg: String,
}

fn add_element(v: &mut Vec<i32>) -> bool {
    v.push(3);
    true
}

fn main() {
    let mut e = get_entity();
    if let Err(errs) = e.validate() {
        println!("The following validations failed: {:?}", errs);
    }
}

It is also possible to use fluent-style validation:

struct Entity {
    id: i32,
    others: Vec<i32>,
    msg: String,
}

impl vale::Validate for Entity {
    #[vale::ruleset]
    fn validate(&mut self) -> vale::Result {
        vale::rule!(self.id > 0, "`id` is nonpositive!");
        vale::rule!(self.others.len() < 5, "Too many others");
    }
}