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
The rules!
macro has two forms, the first being rules!( You can run a list of validator on a single piece of data and get a
Result<(), Vecback using
rules!` single form, which can be done like so:
#[macro_use] extern crate accord; use accord::Error; use accord::validators::{length, contains, 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
.
#[macro_use] extern crate accord; use accord::{MultipleError, MultipleInvalid}; use accord::validators::{length, contains, 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)] }; }