valico/json_schema/validators/
properties.rs

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

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

#[derive(Debug)]
pub enum AdditionalKind {
    Boolean(bool),
    Schema(url::Url)
}

#[allow(missing_copy_implementations)]
pub struct Properties {
    pub properties: collections::HashMap<String, url::Url>,
    pub additional: AdditionalKind,
    pub patterns: Vec<(regex::Regex, url::Url)>
}

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

        'main: for (key, value) in object.iter() {
            let mut is_property_passed = false;
            if self.properties.contains_key(key) {
                let url = self.properties.get(key).unwrap();
                let schema = scope.resolve(url);
                if schema.is_some() {
                    let value_path = [path, key.as_ref()].join("/");
                    state.append(schema.unwrap().validate_in(value, value_path.as_ref()))
                } else {
                    state.missing.push(url.clone())
                }

               is_property_passed = true;
            }

            let mut is_pattern_passed = false;
            for &(ref regex, ref url) in self.patterns.iter() {
                if regex.is_match(key.as_ref()) {
                    let schema = scope.resolve(url);
                    if schema.is_some() {
                        let value_path = [path, key.as_ref()].join("/");
                        state.append(schema.unwrap().validate_in(value, value_path.as_ref()));
                        is_pattern_passed = true;
                    } else {
                        state.missing.push(url.clone())
                    }
                }
            }

            if is_property_passed || is_pattern_passed {
                continue 'main;
            }

            match self.additional {
                AdditionalKind::Boolean(allowed) if allowed == false => {
                    state.errors.push(Box::new(
                        errors::Properties {
                            path: path.to_string(),
                            detail: "Additional properties are not allowed".to_string()
                        }
                    ))
                },
                AdditionalKind::Schema(ref url) => {
                    let schema = scope.resolve(url);

                    if schema.is_some() {
                        let value_path = [path, key.as_ref()].join("/");
                        state.append(schema.unwrap().validate_in(value, value_path.as_ref()))
                    } else {
                        state.missing.push(url.clone())
                    }
                },
                // Additional are allowed here
                _ => ()
            }
        }

        state
    }
}