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
use super::{AllowedType, Attribute};
use crate::definition;
use crate::error::{Error, ValidationError};
use crate::validator::{Context, DocumentPath, State};

use std::collections::HashSet;

use serde_json;

#[derive(Debug)]
pub struct DefinitionType {
    name: String,
    typ: definition::Type,
}

impl DefinitionType {
    pub fn new(path: DocumentPath, ctx: &Context) -> Result<Self, Error> {
        let obj = ctx.raw_definition();
        let typ = definition::Type::new(obj, path.clone())?;
        Ok(DefinitionType {
            name: ctx.name(),
            typ,
        })
    }

    pub fn build(
        _: &mut State,
        path: DocumentPath,
        ctx: &Context,
    ) -> Result<Box<Attribute>, Error> {
        Ok(Box::new(DefinitionType::new(path, ctx)?))
    }

    pub fn allowed_types() -> HashSet<AllowedType> {
        use crate::definition::Type::*;
        let mut set: HashSet<AllowedType> = HashSet::new();
        set.insert(AllowedType::new(Null, true));
        set.insert(AllowedType::new(Boolean, true));
        set.insert(AllowedType::new(String, true));
        set.insert(AllowedType::new(Number, true));
        set.insert(AllowedType::new(Integer, true));
        set.insert(AllowedType::new(Object, true));
        set.insert(AllowedType::new(Array, true));
        set.insert(AllowedType::new(Enum, true));
        set.insert(AllowedType::new(Const, true));
        set.insert(AllowedType::new(Pointer, true));
        set
    }
}

impl Attribute for DefinitionType {
    fn validate(
        &self,
        _: &State,
        path: Vec<String>,
        input: &serde_json::Value,
    ) -> Result<(), ValidationError> {
        use crate::definition::Type::*;

        let err_msg = match self.typ {
            Null => {
                if input.is_null() {
                    return Ok(());
                }
                "Value must be null."
            }
            Boolean => {
                if input.is_boolean() {
                    return Ok(());
                }
                "Value must be boolean."
            }
            String => {
                if input.is_string() {
                    return Ok(());
                }
                "Value must be a string."
            }
            Number => {
                if input.is_number() {
                    return Ok(());
                }
                "Value must be a number."
            }
            Integer => {
                if input.is_i64() {
                    return Ok(());
                }
                "Value must be a integer."
            }
            Object => {
                if input.is_object() {
                    return Ok(());
                }
                "Value must be an object."
            }
            Array => {
                if input.is_array() {
                    return Ok(());
                }
                "Value must be an array."
            }
            _ => return Ok(()), // Enum, const and pointer types rely on other attributes.
        };

        Err(ValidationError::Failure {
            rule: self.name.clone(),
            path: path,
            message: err_msg.to_string(),
        })
    }
}