
validex
A Rust validation library.
Unlike validator library, which use syntex or string.
validex use concrete rust values in #[check(...)] attribute.
Any types that implement Check trait can be used in #[check(...)].
This enables IDE-friendly features like: auto-import/fix, goto-def, syntax highlight, hover docs, etc...
Features
Check derive macros for validating structs.
- Zore-cost abstractions:
All, Any and Not combinators.
- Flexible and Extensible: use functions or any type that implements
Check trait.
- Detailed error reporting: preserves all relevant information.
- IDE friendly: Works well with Rust Analyzer.
Example
Add validex to your Cargo.toml:
[dependencies]
validex = "0.1"
Here is an simple example:
use validex::*;
fn validate_url(_: &impl AsRef<str>) -> Result<(), String> {
Ok(())
}
fn validate_user_id(id: &u32) -> Result<(), &'static str> {
if *id == 13 {
return Err("13 is an unlucky number");
}
Ok(())
}
#[derive(Check)]
struct UserData {
#[check(
Any((
Range(20..=30),
All((Not(45), Range(40..=50))),
100,
)),
validate_user_id
)]
id: u32,
#[check(Maybe((
Not("example.com"),
Length(..=20),
validate_url,
)))]
site: Option<String>,
#[check(Range(13..=28), Not(Range(18..=24)))]
age: u32,
}
#[derive(Check)]
struct User {
#[check(UserData::check)]
data: UserData,
}
fn main() {
let user = User {
data: UserData {
id: 45,
site: Some("personal-blog.net".into()),
age: 25,
},
};
if let Err(err) = user.check() {
println!("{:}", err);
}
}