Macro accord::rules [] [src]

macro_rules! rules {
    ( $a:expr, [ $( $b:expr ),* ] ) => { ... };
    ( $( $a:expr => $b:expr => [ $( $c:expr ),* ] ),* ) => { ... };
}

Runs a list of validators on data.

Examples

You can run a list of validator on a single piece of data and get a Result<(), Vec<Invalid>> back using rules! single form, which can be done like so:

#![cfg_attr(feature = "inclusive_range", feature(inclusive_range, inclusive_range_syntax))]

#[macro_use]
extern crate accord;

use accord::Error;
use accord::validators::{length, contains, range};

#[cfg(not(feature = "inclusive_range"))]
fn main() {
    let email = "test@test.test".to_string();
    let password = "kfjsdkfjsdkfjfksjdfkdsfjs".to_string();
    let age = 25;

    let _ = rules!(email, [length(5, 64), contains("@"), contains(".")]);
    let _ = rules!(password, [length(8, 64)]);
    let _ = rules!(age, [range(12, 127)]);
}

#[cfg(feature = "inclusive_range")]
fn main() {
    let email = "test@test.test".to_string();
    let password = "kfjsdkfjsdkfjfksjdfkdsfjs".to_string();
    let age = 25;

    let _ = rules!(email, [length(5...64), contains("@"), contains(".")]);
    let _ = rules!(password, [length(8...64)]);
    let _ = rules!(age, [range(12...127)]);
}

If you have more than one piece of data to test, you can uses its collection form, which returns a Result<(), Vec<MultipleResult>>.

Notice that in the collection form, you also provide a tag, like email or password. This makes it easy to distingues between all the MultipleInvalids in the Vector.

#![cfg_attr(feature = "inclusive_range", feature(inclusive_range, inclusive_range_syntax))]

#[macro_use]
extern crate accord;

use accord::{MultipleError, MultipleInvalid};
use accord::validators::{length, contains, range};

#[cfg(not(feature = "inclusive_range"))]
fn main() {
    let email = "test@test.test".to_string();
    let password = "kfjsdkfjsdkfjfksjdfkdsfjs".to_string();
    let age = 25;

    let _ = rules!{
        "email" => email => [length(5, 64), contains("@"), contains(".")],
        "password" => password => [length(8, 64)],
        "age" => age => [range(12, 127)]
    };
}

#[cfg(feature = "inclusive_range")]
fn main() {
    let email = "test@test.test".to_string();
    let password = "kfjsdkfjsdkfjfksjdfkdsfjs".to_string();
    let age = 25;

    let _ = rules!{
        "email" => email => [length(5...64), contains("@"), contains(".")],
        "password" => password => [length(8...64)],
        "age" => age => [range(12...127)]
    };
}