-
Notifications
You must be signed in to change notification settings - Fork 315
/
test_helpers.rs
60 lines (55 loc) · 2.42 KB
/
test_helpers.rs
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
use std::{fs::File,
io::Read,
path::PathBuf};
use valico::json_schema;
/// Asserts that `json_string` is valid according to the specified JSON schema.
///
/// In the case of invalid input, all validation errors are neatly
/// rendered, along with a pretty-printed formatting of the
/// offending JSON.
pub fn assert_valid(json_string: &str, schema: &str) {
let result = validate_string(json_string, schema);
if !result.is_valid() {
let error_string = result.errors
.into_iter()
.map(|x| format!(" {:?}", x))
.collect::<Vec<String>>()
.join("\n");
let pretty_json = serde_json::to_string_pretty(&serde_json::from_str::<serde_json::Value>(json_string).expect("JSON should \
parse if we \
get this far")).expect("JSON should serialize if we get this far");
panic!(
r#"
JSON does not validate!
Errors:
{}
JSON:
{}
"#,
error_string, pretty_json
);
}
}
/// Compares the incoming JSON string against the JSON schema
/// and returns the resulting `ValidationState`.
///
/// In general, you should prefer using `assert_valid` directly.
pub fn validate_string(input: &str, schema: &str) -> json_schema::ValidationState {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("doc")
.join(schema);
let mut schema_file = File::open(path).expect("could not open schema file");
let mut raw_schema = String::new();
schema_file.read_to_string(&mut raw_schema)
.expect("could not read schema file");
let parsed_schema: serde_json::Value =
serde_json::from_str(&raw_schema).expect("Could not parse schema as JSON");
let mut scope = json_schema::scope::Scope::new();
// NOTE: using `false` instead of `true` allows us to use
// `$comment` keyword, as well as our own `$deprecated` and
// `$since` keywords.
let schema = scope.compile_and_return(parsed_schema, false)
.expect("Could not compile the schema");
let input_json: serde_json::Value =
serde_json::from_str(input).expect("Could not parse input as JSON");
schema.validate(&input_json)
}