A tool to easily constrain a struct and recover errors.
```toml
[dependencies] type-rules = { version = "0.1.2", features = ["derive", "regex"] } ```
You can declare a struct and impose some constraints on each field and check the validity like this: ```rust use typerules::Validator; //Don't forget to import the used rules. use typerules::rules::{MaxLength, MinMaxLength, RegEx};
struct NewUser { #[rule(MaxLength(100), RegEx(r"^\S+@\S+.\S+"))] email: String, #[rule(MinMaxLength(8, 50))] password: String, }
let newuser = NewUser { email: "examples@examples.com".tostring(), password: "OPw$5%hJ".tostring(), }; assert!(newuser.checkvalidity().isok()); let newuser = NewUser { email: "examples@examples.com".tostring(), password: "O".tostring(), }; assert!(newuser.checkvalidity().iserr()); //Value is too short ```
To check recursively, you can use the Validate
rule
```rust use typerules::rules::{MaxLength, RegEx, Validate, MinMaxLength}; use typerules::Validator;
struct EmailWrapper(#[rule(MaxLength(100), RegEx(r"^\S+@\S+.\S+"))] String);
struct User { #[rule(Validate())] email: EmailWrapper, #[rule(MinMaxLength(8, 50))] password: String, } ```
You can use expressions directly in rule derive attribute.
For example, you can use const or function directly in the checker parameters:
```rust use typerules::rules::MaxRange; use chrono::prelude::*; use typerules::Validator;
struct AnniversaryDate(#[rule(MaxRange(Utc::now()))] DateTime
rust
use typerules::rules::MinLength;
use typerules::Validator;
const MINPASSWORDLENGTH: usize = 8;
struct Password(#[rule(MinLength(MINPASSWORDLENGTH))] String); ```
Or use expressions to express a checker directly. Here is an example of using a rule with more complex values:
```rust use std::env; use typerules::rules::MaxLength; use typerules::Validator;
fn generatemaxpayloadrule() -> MaxLength { MaxLength(match env::var("MAXPAYLOAD") { Ok(val) => val.parse().unwraporelse(|| 10000), Err() => 10000, }) }
struct Payload(#[rule(generatemaxpayload_rule())] String); ```
In this case the generate_max_payload_rule
function is executed at each check
If you need a specific rule, just make a tuple struct (or struct if you make the declaration outside the struct definition)
that implements the Rule
feature :
```rust use typerules::Rule; use typerules::Validator;
struct IsEven();
impl Rule
struct MyInteger(#[rule(IsEven())] i32); ```
Here a list of the rules you can find in this crate, if you want more details go to the rule definition.
Check the length of a String
or &str
:
- MinLength
: Minimum length ex: MinLength(5)
- MaxLength
: Maximum length ex: MaxLength(20)
- MinMaxLength
: Minimum and maximum length ex: MinMaxLength(5, 20)
Check the range for anything that implements PartialOrd<Self>
like all numeric/floating types
or dates with chrono
:
- MinRange
: Minimum range ex: MinRange(5)
- MaxRange
: Maximum range ex: MaxRange(20)
- MinMaxRange
: Minimum and maximum range ex: MinMaxRange(5, 20)
Check the size of a Vec<T>
:
- MinSize
: Minimum size ex: MinSize(5)
- MaxSize
: Maximum size ex: MaxSize(20)
- MinMaxSize
: Minimum and maximum size ex: MinMaxSize(5, 20)
others :
Validate
: Recursive checking ex: Validate()
RegEx
: check if a String
or &str
matches the regex.
You need the regex
feature to use it.
ex: RegEx(r"^\S+@\S+\.\S+")