not-so-fast

A Rust library for data validation.

Features

Installation

bash cargo add not-so-fast # --features derive serde

Available cargo features:

Usage

```rust use notsofast::{Validate, ValidationNode, ValidationError};

[derive(Validate)]

struct User { #[validate(custom = alphaonly, charlength(max = 30))] nick: String, #[validate(range(min = 15, max = 100))] age: u8, #[validate(length(max = 3), items(char_length(max = 50)))] cars: Vec, }

fn alphaonly(s: &str) -> ValidationNode { ValidationNode::errorif( s.chars().any(|c| !c.isalphanumeric()), || ValidationError::withcode("alpha_only") ) }

let user = User { nick: "tom1980".into(), age: 200, cars: vec![ "first".into(), "second".into(), "third".repeat(11), "fourth".into(), ], };

let node = user.validate(); assert!(node.iserr()); asserteq!( vec![ ".age: range: Number not in range: max=100, min=15, value=200", ".cars: length: Invalid length: max=3, value=4", ".cars[2]: charlength: Invalid character length: max=50, value=55", ".nick: alphaonly", ].join("\n"), node.to_string() ); ```

Guides

not-so-fast/examples/manual.rs explains how to write custom validators.

not-so-fast/examples/derive.rs shows how to use Validator derive macro.

Compared to validator

not-so-fast attempts to fix issues I stumbled upon when working with the popular https://github.com/Keats/validator crate. APIs of the two libraries are similar, but not compatible. Here are the differences of not-so-fast:

TODO

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.