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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use serde_json::{Value, to_value};
use url;

use super::super::json_schema;
use super::param;
use super::coercers;
use super::validators;
use super::errors;

pub struct Builder {
    requires: Vec<param::Param>,
    optional: Vec<param::Param>,
    validators: validators::Validators,
    schema_builder: Option<Box<Fn(&mut json_schema::Builder) + Send + Sync>>,
    schema_id: Option<url::Url>
}

unsafe impl Send for Builder { }

impl Builder {

    pub fn new() -> Builder {
        Builder {
            requires: vec![],
            optional: vec![],
            validators: vec![],
            schema_builder: None,
            schema_id: None
        }
    }

    pub fn build<F>(rules: F) -> Builder where F: FnOnce(&mut Builder) {
        let mut builder = Builder::new();
        rules(&mut builder);

        builder
    }

    pub fn get_required(&self) -> &Vec<param::Param> {
        return &self.requires;
    }

    pub fn get_optional(&self) -> &Vec<param::Param> {
        return &self.optional;
    }

    pub fn get_validators(&self) -> &validators::Validators {
        return &self.validators;
    }

    pub fn req_defined(&mut self, name: &str) {
        let params = param::Param::new(name);
        self.requires.push(params);
    }

    pub fn req_typed(&mut self, name: &str, coercer: Box<coercers::Coercer + Send + Sync>) {
        let params = param::Param::new_with_coercer(name, coercer);
        self.requires.push(params);
    }

    pub fn req_nested<F>(&mut self, name: &str, coercer: Box<coercers::Coercer + Send + Sync>, nest_def: F) where F: FnOnce(&mut Builder) {
        let nest_builder = Builder::build(nest_def);
        let params = param::Param::new_with_nest(name, coercer, nest_builder);
        self.requires.push(params);
    }

    pub fn req<F>(&mut self, name: &str, param_builder: F) where F: FnOnce(&mut param::Param) {
        let params = param::Param::build(name, param_builder);
        self.requires.push(params);
    }

    pub fn opt_defined(&mut self, name: &str) {
        let params = param::Param::new(name);
        self.optional.push(params);
    }

    pub fn opt_typed(&mut self, name: &str, coercer: Box<coercers::Coercer + Send + Sync>) {
        let params = param::Param::new_with_coercer(name, coercer);
        self.optional.push(params);
    }

    pub fn opt_nested<F>(&mut self, name: &str, coercer: Box<coercers::Coercer + Send + Sync>, nest_def: F) where F: FnOnce(&mut Builder) {
        let nest_builder = Builder::build(nest_def);
        let params = param::Param::new_with_nest(name, coercer, nest_builder);
        self.optional.push(params);
    }

    pub fn opt<F>(&mut self, name: &str, param_builder: F) where F: FnOnce(&mut param::Param) {
        let params = param::Param::build(name, param_builder);
        self.optional.push(params);
    }

    pub fn validate(&mut self, validator: Box<validators::Validator + 'static + Send + Sync>) {
        self.validators.push(validator);
    }

    pub fn validate_with<F>(&mut self, validator: F) where F: Fn(&Value, &str) -> validators::ValidatorResult + 'static + Send+Sync {
        self.validators.push(Box::new(validator));
    }

    pub fn mutually_exclusive(&mut self, params: &[&str]) {
        let validator = Box::new(validators::MutuallyExclusive::new(params));
        self.validators.push(validator);
    }

    pub fn exactly_one_of(&mut self, params: &[&str]) {
        let validator = Box::new(validators::ExactlyOneOf::new(params));
        self.validators.push(validator);
    }

    pub fn at_least_one_of(&mut self, params: &[&str]) {
        let validator = Box::new(validators::AtLeastOneOf::new(params));
        self.validators.push(validator);
    }

    pub fn schema_id(&mut self, id: url::Url) {
        self.schema_id = Some(id);
    }

    pub fn schema<F>(&mut self, build: F) where F: Fn(&mut json_schema::Builder,) + 'static + Send + Sync {
        self.schema_builder = Some(Box::new(build));
    }

    pub fn build_schemes(&mut self, scope: &mut json_schema::Scope) -> Result<(), json_schema::SchemaError> {
        for param in self.requires.iter_mut().chain(self.optional.iter_mut()) {
            if param.schema_builder.is_some() {
                let json_schema = json_schema::builder::schema_box(param.schema_builder.take().unwrap());
                let id = try!(scope.compile(to_value(&json_schema).unwrap(), true));
                param.schema_id = Some(id);
            }

            if param.nest.is_some() {
                try!(param.nest.as_mut().unwrap().build_schemes(scope));
            }
        }

        if self.schema_builder.is_some() {
            let json_schema = json_schema::builder::schema_box(self.schema_builder.take().unwrap());
            let id = try!(scope.compile(to_value(&json_schema).unwrap(), true));
            self.schema_id = Some(id);
        }

        Ok(())
    }

    pub fn process(&self, val: &mut Value, scope: &Option<&json_schema::Scope>) -> json_schema::ValidationState {
        self.process_nest(val, "", scope)
    }

    pub fn process_nest(&self, val: &mut Value, path: &str, scope: &Option<&json_schema::Scope>) -> json_schema::ValidationState {
        let mut state = if val.is_array() {
            let mut state = json_schema::ValidationState::new();
            let array = val.as_array_mut().unwrap();
            for (idx, item) in array.iter_mut().enumerate() {
                let item_path = [path, idx.to_string().as_ref()].join("/");
                if item.is_object() {
                    let process_state = self.process_object(item, item_path.as_ref(), scope);
                    state.append(process_state);
                } else {
                    state.errors.push(
                        Box::new(errors::WrongType {
                            path: item_path.to_string(),
                            detail: "List value is not and object".to_string()
                        })
                    )
                }
            }

            state
        } else if val.is_object() {
            self.process_object(val, path, scope)
        } else {
            let mut state = json_schema::ValidationState::new();
            state.errors.push(
                Box::new(errors::WrongType {
                    path: path.to_string(),
                    detail: "Value is not an object or an array".to_string()
                })
            );

            state
        };

        let path = if path == "" {
            "/"
        } else {
            path
        };

        if self.schema_id.is_some() && scope.is_some() {
            let id = self.schema_id.as_ref().unwrap();
            let schema = scope.as_ref().unwrap().resolve(id);
            match schema {
                Some(schema) => state.append(schema.validate_in(val, path)),
                None => state.missing.push(id.clone())
            }
        }

        state
    }

    fn process_object(&self, val: &mut Value, path: &str, scope: &Option<&json_schema::Scope>) -> json_schema::ValidationState  {

        let mut state = json_schema::ValidationState::new();

        {
            let object = val.as_object_mut().expect("We expect object here");
            for param in self.requires.iter() {
                let ref name = param.name;
                let present = object.contains_key(name);
                let param_path = [path, name.as_ref()].join("/");
                if present {
                    let process_result = param.process(object.get_mut(name).unwrap(), param_path.as_ref(), scope);
                    match process_result.value  {
                        Some(new_value) => { object.insert(name.clone(), new_value); },
                        None => ()
                    }

                    state.append(process_result.state);
                } else {
                    state.errors.push(Box::new(errors::Required {
                        path: param_path.clone()
                    }))
                }
            }

            for param in self.optional.iter() {
                let ref name = param.name;
                let present = object.contains_key(name);
                let param_path = [path, name.as_ref()].join("/");
                if present {
                    let process_result = param.process(object.get_mut(name).unwrap(), param_path.as_ref(), scope);
                    match process_result.value  {
                        Some(new_value) => { object.insert(name.clone(), new_value); },
                        None => ()
                    }

                    state.append(process_result.state);
                }
            }
        }

        let path = if path == "" {
            "/"
        } else {
            path
        };

        for validator in self.validators.iter() {
            match validator.validate(val, path) {
                Ok(()) => (),
                Err(err) => {
                    state.errors.extend(err);
                }
            };
        }

        {
            if state.is_valid() {
                let object = val.as_object_mut().expect("We expect object here");

                // second pass we need to validate without default values in optionals
                for param in self.optional.iter() {
                    let ref name = param.name;
                    let present = object.contains_key(name);
                    if !present {
                        match param.default.as_ref() {
                            Some(val) => { object.insert(name.clone(), val.clone()); },
                            None => ()
                        };
                    }
                }
            }
        }

        state
    }
}