1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use serde_json::{Value};
use std::fmt;

use common::error;

pub use self::allowed_values::{AllowedValues};
pub use self::at_least_one_of::{AtLeastOneOf};
pub use self::exactly_one_of::{ExactlyOneOf};
pub use self::mutually_exclusive::{MutuallyExclusive};
pub use self::rejected_values::{RejectedValues};

macro_rules! strict_process {
    ($val:expr, $path:ident, $err:expr) => {{
        let maybe_val = $val;
        if maybe_val.is_none() {
            return Err(vec![
                Box::new($crate::json_dsl::errors::WrongType {
                    path: $path.to_string(),
                    detail: $err.to_string()
                })
            ])
        }

        maybe_val.unwrap()
    }}
}

mod allowed_values;
mod at_least_one_of;
mod exactly_one_of;
mod mutually_exclusive;
mod regex;
mod rejected_values;

pub type ValidatorResult = Result<(), error::ValicoErrors>;

pub trait Validator {
    fn validate(&self, item: &Value, &str) -> ValidatorResult;
}

impl fmt::Debug for Validator + 'static {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("[validator]")
    }
}

pub type BoxedValidator = Box<Validator + 'static + Send + Sync>;
pub type Validators = Vec<BoxedValidator>;

impl<T> Validator for T where T: Fn(&Value, &str) -> ValidatorResult {
    fn validate(&self, val: &Value, path: &str) -> ValidatorResult {
        self(val, path)
    }
}