ProductPromotion
Logo

Rust

made by https://0x3d.site

GitHub - tu6ge/valitron: Valitron is a rust validation, support ergonomics, functional and configurable
Valitron is a rust validation, support ergonomics, functional and configurable - tu6ge/valitron
Visit Site

GitHub - tu6ge/valitron: Valitron is a rust validation, support ergonomics, functional and configurable

GitHub - tu6ge/valitron: Valitron is a rust validation, support ergonomics, functional and configurable

Valitron is an ergonomic, functional and configurable validator

In the future, modularization will be supported

Inspired by axum

Features

  • Ergonomics validation
  • Build-in rule, e.g. Required, StartWith ...
  • Closure validate
  • Related validate, e.g. password confirm
  • Custom rule with other parameter
  • Check / modify input data
  • Custom error message type
  • Support different error types convert, it can use both build-in rules and custom error type simultaneously
  • Collect validate error messages
  • Support all types data on #[derive(Serialize, Deserialize)] ( visit serde for more info)

Example 1

fn main() {
    let validator = Validator::new()
        .rule("name", Required.and(StartWith("hello")))
        .rule("age", custom(age_limit))
        .message([
            ("name.required", "name is required"),
            ("name.start_with", "name should be starts with `hello`"),
        ]);

    let person = Person {
        name: "li",
        age: 18,
    };

    let res = validator.validate(person);
    // or using trait
    let res = person.validate(validator);
}

fn age_limit(n: &mut u8) -> Result<(), Message> {
    if *n >= 25 && *n <= 45 {
        return Ok(());
    }
    Err("age should be between 25 and 45".into())
}

Example 2

use valitron::register::string::Validator;
impl Input {
    fn new(mut input: Input) -> Result<Self, Validator<Message>> {
        let valid = Validator::new()
            .insert("name", &mut input.name, Trim.and(Required))
            .insert("email", &mut input.email, Trim.and(Required).and(Email))
            .insert("gender", &mut input.gender, custom(validate_gender))
            .insert(
                "password",
                &mut input.password,
                Trim.custom(validate_password),
            )
            .insert_fn("age", || {
                if input.age < 10 {
                    input.age = 10;
                }
                if input.age > 18 {
                    Err(Message::fallback("age is out of range"))
                } else {
                    Ok(())
                }
            });

        valid.validate(input)
    }
}
fn validate_password(pass: &mut String) -> Result<(), Message> {
    todo!()
}

fn validate_gender(gender: &mut String) -> Result<(), Message> {
    Ok(())
}

Document

Rules Usage

Usage Description
Required one rule
Required.and(StartsWith("foo")) multi rules
Required.and(StartsWith('a')).bail() multi rules and bail
custom(my_handler) custom handler rule
Required.custom(my_handler) rule and handler rule
Not(StartsWith("foo")) negative rule
Required.and(Not(StartsWith("foo"))) negative rule

Bench

second scheme validator(other library)
192.55 ns 680.43 ns

License

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory