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
#![warn(missing_docs)]
use serde_json::Value;
mod config;
mod context;
mod error;
mod format;
mod resolver;
pub mod schemas;
mod unique;
mod util;
mod validators;
pub use crate::config::Config;
use crate::context::Context;
pub use crate::error::{ErrorIterator, ValidationError};
pub fn validate<'a>(
cfg: &'a config::Config<'a>,
instance: &'a Value,
) -> Result<(), ErrorIterator<'a>> {
let mut errors = validators::descend(
cfg,
instance,
cfg.get_schema(),
None,
Context::new_from(cfg.get_schema()),
)
.peekable();
if errors.peek().is_none() {
Ok(())
} else {
Err(Box::new(errors))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::PathBuf;
const KNOWN_FAILURES: &'static [&'static str] = &["refRemote.json"];
fn test_draft(dirname: &str, draft: schemas::Draft) {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("JSON-Schema-Test-Suite/tests");
path.push(dirname);
let paths = fs::read_dir(path).unwrap();
for entry in paths {
let dir_entry = &entry.unwrap();
if KNOWN_FAILURES.contains(&dir_entry.file_name().to_str().unwrap()) {
continue;
}
let path = dir_entry.path();
if path.extension().map_or_else(|| "", |x| x.to_str().unwrap()) == "json" {
println!("Testing {:?}", path.display());
let file = fs::File::open(path).unwrap();
let json: Value = serde_json::from_reader(file).unwrap();
for testset in json.as_array().unwrap().iter() {
println!(
" Test set {}",
testset.get("description").unwrap().as_str().unwrap()
);
let schema = testset.get("schema").unwrap();
let tests = testset.get("tests").unwrap();
for test in tests.as_array().unwrap().iter() {
println!(
" Test {}",
test.get("description").unwrap().as_str().unwrap()
);
let data = test.get("data").unwrap();
let valid = test.get("valid").unwrap();
if let Value::Bool(expected_valid) = valid {
let cfg = config::Config::from_schema(&schema, Some(draft)).unwrap();
assert!(cfg.validate_schema().is_ok());
let result = validate(&cfg, &data);
assert_eq!(result.is_ok(), *expected_valid);
let cfg2 = config::Config::from_schema(&schema, Some(draft)).unwrap();
let result2 = cfg2.validate(&data);
assert!(cfg2.validate_schema().is_ok());
assert_eq!(result2.is_ok(), *expected_valid);
}
}
}
}
}
}
#[test]
fn test_draft7() {
test_draft("draft7", schemas::Draft::Draft7);
}
#[test]
fn test_draft6() {
test_draft("draft6", schemas::Draft::Draft6);
}
#[test]
fn test_draft4() {
test_draft("draft4", schemas::Draft::Draft4);
}
}