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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use serde_json::{Value, to_value};
use serde::{Serialize, Serializer};
use std::fmt;
use url;

use super::scope;

#[macro_export]
macro_rules! strict_process {
    ($val:expr, $path:ident, $err:expr) => {{
        let maybe_val = $val;
        if maybe_val.is_none() {
            return val_error!(
                $crate::json_schema::errors::WrongType {
                    path: $path.to_string(),
                    detail: $err.to_string()
                }
            )
        }

        maybe_val.unwrap()
    }}
}

macro_rules! nonstrict_process {
    ($val:expr, $path:ident) => {{
        let maybe_val = $val;
        if maybe_val.is_none() {
            return $crate::json_schema::validators::ValidationState::new()
        }

        maybe_val.unwrap()
    }}
}

macro_rules! val_error{
    ($err:expr) => (
        $crate::json_schema::validators::ValidationState{
            errors: vec![
                Box::new($err)
            ],
            missing: vec![]
        }
    )
}

pub use self::multiple_of::{MultipleOf};
pub use self::maxmin::{Maximum, Minimum};
pub use self::maxmin_length::{MaxLength, MinLength};
pub use self::pattern::{Pattern};
pub use self::maxmin_items::{MaxItems, MinItems};
pub use self::unique_items::{UniqueItems};
pub use self::items::{Items};
pub use self::maxmin_properties::{MaxProperties, MinProperties};
pub use self::required::{Required};
pub use self::properties::{Properties};
pub use self::dependencies::{Dependencies};
pub use self::enum_::{Enum};
pub use self::type_::{Type};
pub use self::of::{AllOf, AnyOf, OneOf};
pub use self::ref_::{Ref};
pub use self::not::{Not};

mod multiple_of;
mod maxmin;
mod maxmin_length;
mod pattern;
mod maxmin_items;
mod unique_items;
pub mod items;
mod maxmin_properties;
mod required;
pub mod properties;
pub mod dependencies;
mod enum_;
pub mod type_;
mod of;
mod ref_;
mod not;
pub mod formats;

#[derive(Debug)]
pub struct ValidationState {
    pub errors: super::super::common::error::ValicoErrors,
    pub missing: Vec<url::Url>
}

impl ValidationState {
    pub fn new() -> ValidationState {
        ValidationState {
            errors: vec![],
            missing: vec![]
        }
    }

    pub fn is_valid(&self) -> bool {
        self.errors.len() == 0
    }

    pub fn is_strictly_valid(&self) -> bool {
        self.errors.len() == 0 && self.missing.len() == 0
    }

    pub fn append(&mut self, second: ValidationState) {
        self.errors.extend(second.errors);
        self.missing.extend(second.missing);
    }
}

impl Serialize for ValidationState {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        let mut map = ::serde_json::Map::new();
        map.insert("errors".to_string(), Value::Array(
            self.errors.iter().map(|err| to_value(err).unwrap()).collect::<Vec<Value>>()
        ));
        map.insert("missing".to_string(), Value::Array(
            self.missing.iter().map(|url| to_value(&url.to_string()).unwrap()).collect::<Vec<Value>>()
        ));
        Value::Object(map).serialize(serializer)
    }
}

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

impl fmt::Debug for Validator + 'static + Send + Sync {
    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, &scope::Scope) -> ValidationState {
    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> ValidationState {
        self(val, path, scope)
    }
}