valico/json_schema/validators/
dependencies.rs

1use std::collections;
2use serde_json::{Value};
3use url;
4
5use super::super::errors;
6use super::super::scope;
7
8#[derive(Debug)]
9pub enum DepKind {
10    Schema(url::Url),
11    Property(Vec<String>)
12}
13
14#[allow(missing_copy_implementations)]
15pub struct Dependencies {
16    pub items: collections::HashMap<String, DepKind>
17}
18
19impl super::Validator for Dependencies {
20    fn validate(&self, object: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
21        if !object.is_object() {
22            return super::ValidationState::new()
23        }
24
25        let mut state = super::ValidationState::new();
26
27        for (key, dep) in self.items.iter() {
28            if object.get(&key).is_some() {
29                match dep {
30                    &DepKind::Schema(ref url) => {
31                        let schema = scope.resolve(url);
32                        if schema.is_some() {
33                            state.append(schema.unwrap().validate_in(object, path));
34                        } else {
35                            state.missing.push(url.clone())
36                        }
37                    },
38                    &DepKind::Property(ref keys) => {
39                        for key in keys.iter() {
40                            if !object.get(&key).is_some() {
41                                state.errors.push(Box::new(
42                                    errors::Required {
43                                        path: [path, key.as_ref()].join("/")
44                                    }
45                                ))
46                            }
47                        }
48                    }
49                }
50            }
51        }
52
53        state
54    }
55}