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
use serde_json::{Value};
use url;

use super::super::errors;
use super::super::scope;

#[allow(missing_copy_implementations)]
pub struct AllOf {
    pub schemes: Vec<url::Url>,
}

impl super::Validator for AllOf {
    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
        let mut state = super::ValidationState::new();

        for url in self.schemes.iter() {
            let schema = scope.resolve(url);

            if schema.is_some() {
                state.append(schema.unwrap().validate_in(val, path))
            } else {
                state.missing.push(url.clone())
            }
        }

        state
    }
}

#[allow(missing_copy_implementations)]
pub struct AnyOf {
    pub schemes: Vec<url::Url>,
}

impl super::Validator for AnyOf {
    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
        let mut state = super::ValidationState::new();

        let mut states = vec![];
        let mut valid = false;
        for url in self.schemes.iter() {
            let schema = scope.resolve(url);

            if schema.is_some() {
                let current_state = schema.unwrap().validate_in(val, path);

                state.missing.extend(current_state.missing.clone());

                if current_state.is_valid() {
                    valid = true;
                    break;
                } else {
                   states.push(current_state)
                }
            } else {
                state.missing.push(url.clone())
            }
        }

        if !valid {
            state.errors.push(Box::new(
                errors::AnyOf {
                    path: path.to_string(),
                    states: states
                }
            ))
        }


        state
    }
}

#[allow(missing_copy_implementations)]
pub struct OneOf {
    pub schemes: Vec<url::Url>,
}

impl super::Validator for OneOf {
    fn validate(&self, val: &Value, path: &str, scope: &scope::Scope) -> super::ValidationState {
        let mut state = super::ValidationState::new();

        let mut states = vec![];
        let mut valid = 0;
        for url in self.schemes.iter() {
            let schema = scope.resolve(url);

            if schema.is_some() {
                let current_state = schema.unwrap().validate_in(val, path);

                state.missing.extend(current_state.missing.clone());

                if current_state.is_valid() {
                    valid += 1;
                } else {
                   states.push(current_state)
                }
            } else {
                state.missing.push(url.clone())
            }
        }

        if valid != 1 {
            state.errors.push(Box::new(
                errors::OneOf {
                    path: path.to_string(),
                    states: states
                }
            ))
        }


        state
    }
}