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
use crate::registry::Registry;
use crate::vm::validate;
use failure::Error;
use json_pointer::JsonPointer;
use std::borrow::Cow;
use url::Url;
pub struct Validator<'a> {
config: Config,
registry: &'a Registry,
}
impl<'a> Validator<'a> {
pub fn new(registry: &'a Registry) -> Self {
Self::new_with_config(Config::default(), registry)
}
pub fn new_with_config(config: Config, registry: &'a Registry) -> Self {
Self { config, registry }
}
pub fn validate(
&'a self,
instance: &'a serde_json::Value,
) -> Result<Vec<ValidationError<'a>>, Error> {
self.validate_by_id(&None, instance)
}
pub fn validate_by_id(
&'a self,
id: &'a Option<Url>,
instance: &'a serde_json::Value,
) -> Result<Vec<ValidationError<'a>>, Error> {
validate(
self.config.max_errors,
self.config.max_depth,
self.config.strict_instance_semantics,
self.registry,
id,
instance,
)
}
}
pub struct Config {
max_errors: usize,
max_depth: usize,
strict_instance_semantics: bool,
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn max_errors(&mut self, max_errors: usize) -> &mut Self {
self.max_errors = max_errors;
self
}
pub fn max_depth(&mut self, max_depth: usize) -> &mut Self {
self.max_depth = max_depth;
self
}
pub fn strict_instance_semantics(&mut self, strict_instance_semantics: bool) -> &mut Self {
self.strict_instance_semantics = strict_instance_semantics;
self
}
}
impl Default for Config {
fn default() -> Self {
Self {
max_errors: 0,
max_depth: 32,
strict_instance_semantics: false,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ValidationError<'a> {
instance_path: JsonPointer<Cow<'a, str>, Vec<Cow<'a, str>>>,
schema_path: JsonPointer<Cow<'a, str>, Vec<Cow<'a, str>>>,
schema_id: &'a Option<Url>,
}
impl<'a> ValidationError<'a> {
pub fn new(
instance_path: JsonPointer<Cow<'a, str>, Vec<Cow<'a, str>>>,
schema_path: JsonPointer<Cow<'a, str>, Vec<Cow<'a, str>>>,
schema_id: &'a Option<Url>,
) -> ValidationError<'a> {
ValidationError {
instance_path,
schema_path,
schema_id,
}
}
pub fn instance_path(&self) -> &JsonPointer<Cow<'a, str>, Vec<Cow<'a, str>>> {
&self.instance_path
}
pub fn schema_path(&self) -> &JsonPointer<Cow<'a, str>, Vec<Cow<'a, str>>> {
&self.schema_path
}
pub fn schema_id(&self) -> &Option<Url> {
&self.schema_id
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::schema::Schema;
use serde_json::json;
#[test]
fn infinite_loop() -> Result<(), Error> {
let mut registry = Registry::new();
registry.register(Schema::from_serde(serde_json::from_value(json!({
"ref": "#",
}))?)?)?;
let validator = Validator::new(®istry);
assert!(validator.validate(&json!({})).is_err());
Ok(())
}
#[test]
fn max_errors() -> Result<(), Error> {
let mut registry = Registry::new();
registry.register(Schema::from_serde(serde_json::from_value(json!({
"elements": { "type": "string" },
}))?)?)?;
let mut config = Config::new();
config.max_errors(3);
let validator = Validator::new_with_config(config, ®istry);
assert_eq!(
validator
.validate(&json!([null, null, null, null, null,]))
.unwrap()
.len(),
3
);
Ok(())
}
}