Crate validex

Crate validex 

Source
Expand description

Crates.io Documentation License: MIT

§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);
    }
}

Modules§

errors
Error types used by the validation checks.

Structs§

All
A condition that requires all sub-conditions to be met.
Any
A condition that requires any sub-condition to be met.
Length
Checks if the length of input is within a specified range.
Maybe
The Maybe combinator allows a validation field to be optional (Option)
Not
Inverts the result of a sub-condition.
Range
Checks if a value is within a specified range.

Traits§

Check
Trait for performing a check on a field.
Verify
A trait that powers conditional combinators Not, All, Any

Type Aliases§

DynError
A dynamic error type.

Derive Macros§

Check