[][src]Trait cherries::validate::Validate

pub trait Validate<T: Clone + Debug> {
    fn validate<IntoString, Predicate>(
        self,
        msg: IntoString,
        predicate: Predicate
    ) -> ValidateChain<T>
    where
        IntoString: Into<String>,
        Predicate: FnOnce(&T) -> bool
; }

Trait: Validate

Provides method validate.

Required methods

fn validate<IntoString, Predicate>(
    self,
    msg: IntoString,
    predicate: Predicate
) -> ValidateChain<T> where
    IntoString: Into<String>,
    Predicate: FnOnce(&T) -> bool

Loading content...

Implementors

impl<T: Clone + Debug> Validate<T> for Cherry<T>[src]

Trait: Validate for Cherry<T>

Provides validate function. self.validate(predicate) returns ValidateProxy<T>. ValidateProxy<T> also has validate to chain for validation.

Examples

extern crate cherries;
use cherries::{node::Leaf, validate::{Validate, Error}};
extern crate uom;
use uom::si::{f32::*, length::meter, area::square_meter};

fn main() {
   let x = Leaf::new()
       .name("x")
       .value(Length::new::<meter>(2.0))
       .build();
   let y = Leaf::new()
       .name("y")
       .value(Length::new::<meter>(1.0))
       .build();
   let res = x * y;
   let validated = res
       .validate("must be less than 1.0!!", |quantity| {
           quantity < &Area::new::<square_meter>(1.0)
       })
       .into_result();
   assert_eq!(
       Err(Error {
           label: "(mul)".to_string(),
           msg: vec!["must be less than 1.0!!".to_string()],
           tree: "json tree".to_string(),
       }),
       validated
   );
}

impl<T: Clone + Debug> Validate<T> for ValidateChain<T>[src]

For validation chaining.

self.validate(predicate) returns ValidateProxy<T>.

Examples

extern crate cherries;
use cherries::{node::Leaf, validate::{Validate, Error}};
extern crate uom;
use uom::si::{f32::*, length::meter, area::square_meter};

fn main() {
   let x = Leaf::new()
       .name("x")
       .value(Length::new::<meter>(2.0))
       .build();
   let y = Leaf::new()
       .name("y")
       .value(Length::new::<meter>(1.0))
       .build();
   let res = x * y;
   let validated = res
       .validate("must be less than 1.0!!", |quantity| {
           quantity < &Area::new::<square_meter>(1.0)
       })
       .validate("must be less than 0.0!!", |quantity| {
           quantity < &Area::new::<square_meter>(0.0)
       })
       .into_result();
   assert_eq!(
       Err(Error {
           label: "(mul)".to_string(),
           msg: vec![
                "must be less than 1.0!!".to_string(),
                "must be less than 0.0!!".to_string()
           ],
           tree: "json tree".to_string(),
       }),
       validated
   );
}
Loading content...