Expand description
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 support for more webframeworks is desired, it
should be fairly trivial to implement support for those frameworks.
§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");
}
}Macros§
- The rule macro is used to create new rules that dictate how a field of the validated entity should be tranformed and validated.
Structs§
- A struct that can be used in
Rocketroutes. If you have some type that implementsValidate, you can designate in your controller that you want to have a validated instance of that type.
Traits§
- The core trait of this library. Any entity that implements
Validatecan be validated by running thevalidatefunction. This will either return anOk(()), or anErrcontaining a list of errors that were triggered during validation. It is also possible forvalidateto perform tranformations on the entity that is being validated.
Type Aliases§
- A type alias for the
Resultreturned by theValidate::validatefunction.
Attribute Macros§
- Use this macro to annotate yout implementation of
vale::Validatefor your struct to help write the error reporting boilerplate for you. See the documentation ofvale::rulefor usage examples.
Derive Macros§
- A proc macro used to implement
Validateautomatically for a struct.