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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use serde_json;
use serde_json::Value;

use format;
use format::FormatChecker;
use validators;
use validators::Validator;

pub trait Draft {
    fn get_validator(&self, key: &str) -> Option<Validator>;
    fn get_schema(&self) -> &'static Value;
    fn get_format_checker(&self, format: &str) -> Option<FormatChecker>;
    fn get_draft_number(&self) -> u8;
}

pub struct Draft7;

impl Draft for Draft7 {
    fn get_validator(&self, key: &str) -> Option<Validator> {
        match key {
            "$ref" => Some(validators::ref_ as Validator),
            "additionalItems" => Some(validators::additionalItems as Validator),
            "additionalProperties" => Some(validators::additionalProperties as Validator),
            "allOf" => Some(validators::allOf as Validator),
            "anyOf" => Some(validators::anyOf as Validator),
            "const" => Some(validators::const_ as Validator),
            "contains" => Some(validators::contains as Validator),
            "dependencies" => Some(validators::dependencies as Validator),
            "enum" => Some(validators::enum_ as Validator),
            "exclusiveMaximum" => Some(validators::exclusiveMaximum as Validator),
            "exclusiveMinimum" => Some(validators::exclusiveMinimum as Validator),
            "format" => Some(validators::format as Validator),
            "if" => Some(validators::if_ as Validator),
            "items" => Some(validators::items as Validator),
            "maxItems" => Some(validators::maxItems as Validator),
            "maxLength" => Some(validators::maxLength as Validator),
            "maxProperties" => Some(validators::maxProperties as Validator),
            "maximum" => Some(validators::maximum as Validator),
            "minItems" => Some(validators::minItems as Validator),
            "minLength" => Some(validators::minLength as Validator),
            "minProperties" => Some(validators::minProperties as Validator),
            "minimum" => Some(validators::minimum as Validator),
            "multipleOf" => Some(validators::multipleOf as Validator),
            "not" => Some(validators::not as Validator),
            "oneOf" => Some(validators::oneOf as Validator),
            "pattern" => Some(validators::pattern as Validator),
            "patternProperties" => Some(validators::patternProperties as Validator),
            "properties" => Some(validators::properties as Validator),
            "propertyNames" => Some(validators::propertyNames as Validator),
            "required" => Some(validators::required as Validator),
            "type" => Some(validators::type_ as Validator),
            "uniqueItems" => Some(validators::uniqueItems as Validator),
            _ => None,
        }
    }

    fn get_schema(&self) -> &'static Value {
        lazy_static! {
            static ref DRAFT7: Value = serde_json::from_str(include_str!("draft7.json")).unwrap();
        }
        &DRAFT7
    }

    fn get_format_checker(&self, key: &str) -> Option<FormatChecker> {
        match key {
            "date" => Some(format::date as FormatChecker),
            "date-time" => Some(format::datetime as FormatChecker),
            "email" => Some(format::email as FormatChecker),
            "hostname" => Some(format::hostname as FormatChecker),
            "idn-email" => Some(format::email as FormatChecker),
            "ipv4" => Some(format::ipv4 as FormatChecker),
            "ipv6" => Some(format::ipv6 as FormatChecker),
            "iri" => Some(format::iri as FormatChecker),
            "iri-reference" => Some(format::iri_reference as FormatChecker),
            "json-pointer" => Some(format::json_pointer as FormatChecker),
            "regex" => Some(format::regex as FormatChecker),
            "time" => Some(format::time as FormatChecker),
            "uri" => Some(format::uri as FormatChecker),
            "uri-reference" => Some(format::uri_reference as FormatChecker),
            "uri-template" => Some(format::uri_template as FormatChecker),
            _ => None,
        }
    }

    fn get_draft_number(&self) -> u8 {
        7
    }
}

pub struct Draft6;

impl Draft for Draft6 {
    fn get_validator(&self, key: &str) -> Option<Validator> {
        match key {
            "$ref" => Some(validators::ref_ as Validator),
            "additionalItems" => Some(validators::additionalItems as Validator),
            "additionalProperties" => Some(validators::additionalProperties as Validator),
            "allOf" => Some(validators::allOf as Validator),
            "anyOf" => Some(validators::anyOf as Validator),
            "const" => Some(validators::const_ as Validator),
            "contains" => Some(validators::contains as Validator),
            "dependencies" => Some(validators::dependencies as Validator),
            "enum" => Some(validators::enum_ as Validator),
            "exclusiveMaximum" => Some(validators::exclusiveMaximum as Validator),
            "exclusiveMinimum" => Some(validators::exclusiveMinimum as Validator),
            "format" => Some(validators::format as Validator),
            "items" => Some(validators::items as Validator),
            "maxItems" => Some(validators::maxItems as Validator),
            "maxLength" => Some(validators::maxLength as Validator),
            "maxProperties" => Some(validators::maxProperties as Validator),
            "maximum" => Some(validators::maximum as Validator),
            "minItems" => Some(validators::minItems as Validator),
            "minLength" => Some(validators::minLength as Validator),
            "minProperties" => Some(validators::minProperties as Validator),
            "minimum" => Some(validators::minimum as Validator),
            "multipleOf" => Some(validators::multipleOf as Validator),
            "not" => Some(validators::not as Validator),
            "oneOf" => Some(validators::oneOf as Validator),
            "pattern" => Some(validators::pattern as Validator),
            "patternProperties" => Some(validators::patternProperties as Validator),
            "properties" => Some(validators::properties as Validator),
            "propertyNames" => Some(validators::propertyNames as Validator),
            "required" => Some(validators::required as Validator),
            "type" => Some(validators::type_ as Validator),
            "uniqueItems" => Some(validators::uniqueItems as Validator),
            _ => None,
        }
    }

    fn get_schema(&self) -> &'static Value {
        lazy_static! {
            static ref DRAFT6: Value = serde_json::from_str(include_str!("draft6.json")).unwrap();
        }
        &DRAFT6
    }

    fn get_format_checker(&self, key: &str) -> Option<FormatChecker> {
        match key {
            "date" => Some(format::date as FormatChecker),
            "date-time" => Some(format::datetime as FormatChecker),
            "email" => Some(format::email as FormatChecker),
            "hostname" => Some(format::hostname as FormatChecker),
            "ipv4" => Some(format::ipv4 as FormatChecker),
            "ipv6" => Some(format::ipv6 as FormatChecker),
            "json-pointer" => Some(format::json_pointer as FormatChecker),
            "regex" => Some(format::regex as FormatChecker),
            "time" => Some(format::time as FormatChecker),
            "uri" => Some(format::uri as FormatChecker),
            "uri-reference" => Some(format::uri_reference as FormatChecker),
            "uri-template" => Some(format::uri_template as FormatChecker),
            _ => None,
        }
    }

    fn get_draft_number(&self) -> u8 {
        6
    }
}

pub struct Draft4;

impl Draft for Draft4 {
    fn get_validator(&self, key: &str) -> Option<Validator> {
        match key {
            "$ref" => Some(validators::ref_ as Validator),
            "additionalItems" => Some(validators::additionalItems as Validator),
            "additionalProperties" => Some(validators::additionalProperties as Validator),
            "allOf" => Some(validators::allOf as Validator),
            "anyOf" => Some(validators::anyOf as Validator),
            "dependencies" => Some(validators::dependencies as Validator),
            "enum" => Some(validators::enum_ as Validator),
            "format" => Some(validators::format as Validator),
            "items" => Some(validators::items as Validator),
            "maxItems" => Some(validators::maxItems as Validator),
            "maxLength" => Some(validators::maxLength as Validator),
            "maxProperties" => Some(validators::maxProperties as Validator),
            "maximum" => Some(validators::maximum_draft4 as Validator),
            "minItems" => Some(validators::minItems as Validator),
            "minLength" => Some(validators::minLength as Validator),
            "minProperties" => Some(validators::minProperties as Validator),
            "minimum" => Some(validators::minimum_draft4 as Validator),
            "multipleOf" => Some(validators::multipleOf as Validator),
            "not" => Some(validators::not as Validator),
            "oneOf" => Some(validators::oneOf as Validator),
            "pattern" => Some(validators::pattern as Validator),
            "patternProperties" => Some(validators::patternProperties as Validator),
            "properties" => Some(validators::properties as Validator),
            "required" => Some(validators::required as Validator),
            "type" => Some(validators::type_ as Validator),
            "uniqueItems" => Some(validators::uniqueItems as Validator),
            _ => None,
        }
    }

    fn get_schema(&self) -> &'static Value {
        lazy_static! {
            static ref DRAFT4: Value = serde_json::from_str(include_str!("draft4.json")).unwrap();
        }
        &DRAFT4
    }

    fn get_format_checker(&self, key: &str) -> Option<FormatChecker> {
        match key {
            "date-time" => Some(format::datetime as FormatChecker),
            "email" => Some(format::email as FormatChecker),
            "hostname" => Some(format::hostname as FormatChecker),
            "ipv4" => Some(format::ipv4 as FormatChecker),
            "ipv6" => Some(format::ipv6 as FormatChecker),
            "regex" => Some(format::regex as FormatChecker),
            "uri" => Some(format::uri as FormatChecker),
            _ => None,
        }
    }

    fn get_draft_number(&self) -> u8 {
        4
    }
}

pub fn draft_from_url(url: &str) -> Option<&Draft> {
    match url {
        "http://json-schema.org/draft-07/schema" => Some(&Draft7),
        "http://json-schema.org/draft-06/schema" => Some(&Draft6),
        "http://json-schema.org/draft-04/schema" => Some(&Draft4),
        _ => None,
    }
}

pub fn draft_from_schema(schema: &Value) -> Option<&Draft> {
    schema
        .as_object()
        .and_then(|x| x.get("$schema"))
        .and_then(Value::as_str)
        .and_then(|x| draft_from_url(x))
}