valico/json_schema/validators/
of.rs

1use serde_json::{Value};
2use url;
3
4use super::super::errors;
5use super::super::scope;
6
7#[allow(missing_copy_implementations)]
8pub struct AllOf {
9    pub schemes: Vec<url::Url>,
10}
11
12impl super::Validator for AllOf {
13    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
14        let mut state = super::ValidationState::new();
15
16        for url in self.schemes.iter() {
17            let schema = scope.resolve(url);
18
19            if schema.is_some() {
20                state.append(schema.unwrap().validate_in(val, path))
21            } else {
22                state.missing.push(url.clone())
23            }
24        }
25
26        state
27    }
28}
29
30#[allow(missing_copy_implementations)]
31pub struct AnyOf {
32    pub schemes: Vec<url::Url>,
33}
34
35impl super::Validator for AnyOf {
36    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
37        let mut state = super::ValidationState::new();
38
39        let mut states = vec![];
40        let mut valid = false;
41        for url in self.schemes.iter() {
42            let schema = scope.resolve(url);
43
44            if schema.is_some() {
45                let current_state = schema.unwrap().validate_in(val, path);
46
47                state.missing.extend(current_state.missing.clone());
48
49                if current_state.is_valid() {
50                    valid = true;
51                    break;
52                } else {
53                   states.push(current_state)
54                }
55            } else {
56                state.missing.push(url.clone())
57            }
58        }
59
60        if !valid {
61            state.errors.push(Box::new(
62                errors::AnyOf {
63                    path: path.to_string(),
64                    states: states
65                }
66            ))
67        }
68
69
70        state
71    }
72}
73
74#[allow(missing_copy_implementations)]
75pub struct OneOf {
76    pub schemes: Vec<url::Url>,
77}
78
79impl super::Validator for OneOf {
80    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
81        let mut state = super::ValidationState::new();
82
83        let mut states = vec![];
84        let mut valid = 0;
85        for url in self.schemes.iter() {
86            let schema = scope.resolve(url);
87
88            if schema.is_some() {
89                let current_state = schema.unwrap().validate_in(val, path);
90
91                state.missing.extend(current_state.missing.clone());
92
93                if current_state.is_valid() {
94                    valid += 1;
95                } else {
96                   states.push(current_state)
97                }
98            } else {
99                state.missing.push(url.clone())
100            }
101        }
102
103        if valid != 1 {
104            state.errors.push(Box::new(
105                errors::OneOf {
106                    path: path.to_string(),
107                    states: states
108                }
109            ))
110        }
111
112
113        state
114    }
115}