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
use crate::{compilation::context::CompilationContext, keywords};
use serde_json::{Map, Value};
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Draft {
Draft4,
Draft6,
Draft7,
}
impl Default for Draft {
fn default() -> Self {
Draft::Draft7
}
}
type CompileFunc =
fn(&Map<String, Value>, &Value, &CompilationContext) -> Option<keywords::CompilationResult>;
impl Draft {
pub(crate) fn get_validator(self, keyword: &str) -> Option<CompileFunc> {
match keyword {
"additionalItems" => Some(keywords::additional_items::compile),
"additionalProperties" => Some(keywords::additional_properties::compile),
"allOf" => Some(keywords::all_of::compile),
"anyOf" => Some(keywords::any_of::compile),
"const" => match self {
Draft::Draft4 => None,
Draft::Draft6 | Draft::Draft7 => Some(keywords::const_::compile),
},
"contains" => match self {
Draft::Draft4 => None,
Draft::Draft6 | Draft::Draft7 => Some(keywords::contains::compile),
},
"contentMediaType" => match self {
Draft::Draft7 | Draft::Draft6 => Some(keywords::content::compile_media_type),
Draft::Draft4 => None,
},
"contentEncoding" => match self {
Draft::Draft7 | Draft::Draft6 => Some(keywords::content::compile_content_encoding),
Draft::Draft4 => None,
},
"dependencies" => Some(keywords::dependencies::compile),
"enum" => Some(keywords::enum_::compile),
"exclusiveMaximum" => match self {
Draft::Draft7 | Draft::Draft6 => Some(keywords::exclusive_maximum::compile),
Draft::Draft4 => None,
},
"exclusiveMinimum" => match self {
Draft::Draft7 | Draft::Draft6 => Some(keywords::exclusive_minimum::compile),
Draft::Draft4 => None,
},
"format" => Some(keywords::format::compile),
"if" => match self {
Draft::Draft7 => Some(keywords::if_::compile),
Draft::Draft6 | Draft::Draft4 => None,
},
"items" => Some(keywords::items::compile),
"maximum" => match self {
Draft::Draft4 => Some(keywords::legacy::maximum_draft_4::compile),
Draft::Draft6 | Draft::Draft7 => Some(keywords::maximum::compile),
},
"maxItems" => Some(keywords::max_items::compile),
"maxLength" => Some(keywords::max_length::compile),
"maxProperties" => Some(keywords::max_properties::compile),
"minimum" => match self {
Draft::Draft4 => Some(keywords::legacy::minimum_draft_4::compile),
Draft::Draft6 | Draft::Draft7 => Some(keywords::minimum::compile),
},
"minItems" => Some(keywords::min_items::compile),
"minLength" => Some(keywords::min_length::compile),
"minProperties" => Some(keywords::min_properties::compile),
"multipleOf" => Some(keywords::multiple_of::compile),
"not" => Some(keywords::not::compile),
"oneOf" => Some(keywords::one_of::compile),
"pattern" => Some(keywords::pattern::compile),
"patternProperties" => Some(keywords::pattern_properties::compile),
"properties" => Some(keywords::properties::compile),
"propertyNames" => match self {
Draft::Draft4 => None,
Draft::Draft6 | Draft::Draft7 => Some(keywords::property_names::compile),
},
"required" => Some(keywords::required::compile),
"type" => match self {
Draft::Draft4 => Some(keywords::legacy::type_draft_4::compile),
Draft::Draft6 | Draft::Draft7 => Some(keywords::type_::compile),
},
"uniqueItems" => Some(keywords::unique_items::compile),
_ => None,
}
}
}
#[inline]
pub(crate) fn draft_from_url(url: &str) -> Option<Draft> {
match url {
"http://json-schema.org/draft-07/schema#" => Some(Draft::Draft7),
"http://json-schema.org/draft-06/schema#" => Some(Draft::Draft6),
"http://json-schema.org/draft-04/schema#" => Some(Draft::Draft4),
_ => None,
}
}
#[inline]
pub(crate) fn draft_from_schema(schema: &Value) -> Option<Draft> {
schema
.get("$schema")
.and_then(Value::as_str)
.and_then(draft_from_url)
}
#[inline]
pub(crate) fn id_of(draft: Draft, schema: &Value) -> Option<&str> {
if let Value::Object(object) = schema {
if draft == Draft::Draft4 {
object.get("id")
} else {
object.get("$id")
}
.and_then(Value::as_str)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{json, Value};
use test_case::test_case;
#[test_case(&json!({"$schema": "http://json-schema.org/draft-07/schema#"}), Some(Draft::Draft7))]
#[test_case(&json!({"$schema": "http://json-schema.org/draft-06/schema#"}), Some(Draft::Draft6))]
#[test_case(&json!({"$schema": "http://json-schema.org/draft-04/schema#"}), Some(Draft::Draft4))]
#[test_case(&json!({"$schema": "http://example.com/custom/schema#"}), None)]
fn test_draft_from_schema(schema: &Value, draft: Option<Draft>) {
assert_eq!(draft_from_schema(schema), draft)
}
#[test]
fn test_default() {
assert_eq!(Draft::default(), Draft::Draft7)
}
}