1 use anyhow
::{bail, Error}
;
2 use serde_json
::{json, Value}
;
6 static STRING_SCHEMA
: Schema
= StringSchema
::new("A test string").schema();
8 static SIMPLE_OBJECT_SCHEMA
: Schema
= ObjectSchema
::new(
9 "simple object schema",
11 ("prop1", false, &STRING_SCHEMA
),
12 ("prop2", true, &STRING_SCHEMA
),
13 ("prop3", false, &STRING_SCHEMA
),
18 static SIMPLE_PROPERTY_STRING_SCHEMA
: Schema
= StringSchema
::new("simple property string")
19 .format(&ApiStringFormat
::PropertyString(&SIMPLE_OBJECT_SCHEMA
))
22 static SIMPLE_ARRAY_SCHEMA
: Schema
= ArraySchema
::new("String list.", &STRING_SCHEMA
).schema();
24 static NESTED_OBJECT_SCHEMA
: Schema
= ObjectSchema
::new(
25 "nested object schema",
27 ("arr1", false, &SIMPLE_ARRAY_SCHEMA
),
28 ("obj1", false, &SIMPLE_OBJECT_SCHEMA
),
29 ("prop1", false, &STRING_SCHEMA
),
34 static NESTED_PROPERTY_SCHEMA
: Schema
= ObjectSchema
::new(
35 "object with property strings",
36 &[("ps1", false, &SIMPLE_PROPERTY_STRING_SCHEMA
)],
40 static ANOTHER_OBJECT_SCHEMA
: Schema
= ObjectSchema
::new(
41 "another simple object schema",
43 ("another1", false, &STRING_SCHEMA
),
44 ("another2", true, &STRING_SCHEMA
),
49 static OBJECT_WITH_ADDITIONAL
: Schema
= ObjectSchema
::new(
50 "object allowing additional properties",
52 ("regular1", false, &STRING_SCHEMA
),
53 ("regular2", true, &STRING_SCHEMA
),
56 .additional_properties(true)
59 static ALL_OF_SCHEMA_NO_ADDITIONAL
: Schema
= AllOfSchema
::new(
60 "flattening 2 objects together",
61 &[&SIMPLE_OBJECT_SCHEMA
, &ANOTHER_OBJECT_SCHEMA
],
65 static ALL_OF_SCHEMA_ADDITIONAL
: Schema
= AllOfSchema
::new(
66 "flattening 2 objects together where 1 allows additional properties",
67 &[&SIMPLE_OBJECT_SCHEMA
, &OBJECT_WITH_ADDITIONAL
],
71 fn compare_error(expected
: &[(&str, &str)], err
: Error
) -> Result
<(), Error
> {
72 let err
= match err
.downcast_ref
::<ParameterError
>() {
74 None
=> bail
!("unable to downcast error: {}", err
),
77 let result
= (move || {
78 let errors
= err
.errors();
80 if errors
.len() != expected
.len() {
82 "error list has different length: {} != {}",
88 for i
in 0..expected
.len() {
89 if expected
[i
].0 != errors
[i
].0 {
91 "error {} path differs: '{}' != '{}'",
97 if expected
[i
].1 != errors
[i
].1.to_string() {
99 "error {} message differs: '{}' != '{}'",
111 println
!("GOT: {:?}", err
);
120 expected_errors
: &[(&str, &str)],
121 ) -> Result
<(), Error
> {
122 match schema
.verify_json(data
) {
123 Ok(_
) => bail
!("expected errors, but got Ok()"),
124 Err(err
) => compare_error(expected_errors
, err
)?
,
130 fn verify_simple_object() -> Result
<(), Error
> {
131 let simple_value
= json
!({"prop1": 1, "prop4": "abc"}
);
134 &SIMPLE_OBJECT_SCHEMA
,
137 ("prop1", "Expected string value."),
138 ("prop4", "schema does not allow additional properties"),
139 ("prop3", "property is missing and it is not optional"),
147 fn verify_nested_object1() -> Result
<(), Error
> {
148 let nested_value
= json
!({"prop1": 1, "prop4": "abc"}
);
151 &NESTED_OBJECT_SCHEMA
,
154 ("prop1", "Expected string value."),
155 ("prop4", "schema does not allow additional properties"),
156 ("arr1", "property is missing and it is not optional"),
157 ("obj1", "property is missing and it is not optional"),
165 fn verify_nested_object2() -> Result
<(), Error
> {
166 let nested_value
= json
!({"prop1": 1, "prop4": "abc", "obj1": {}
, "arr1": ["abc", 0]});
169 &NESTED_OBJECT_SCHEMA
,
172 ("arr1/[1]", "Expected string value."),
173 ("obj1/prop1", "property is missing and it is not optional"),
174 ("obj1/prop3", "property is missing and it is not optional"),
175 ("prop1", "Expected string value."),
176 ("prop4", "schema does not allow additional properties"),
184 fn verify_nested_property1() -> Result
<(), Error
> {
185 let value
= json
!({"ps1": "abc"}
);
188 &NESTED_PROPERTY_SCHEMA
,
192 "value without key, but schema does not define a default key",
200 fn verify_nested_property2() -> Result
<(), Error
> {
201 let value
= json
!({"ps1": "abc=1"}
);
204 &NESTED_PROPERTY_SCHEMA
,
207 ("ps1/abc", "schema does not allow additional properties"),
208 ("ps1/prop1", "property is missing and it is not optional"),
209 ("ps1/prop3", "property is missing and it is not optional"),
217 fn verify_nested_property3() -> Result
<(), Error
> {
218 let value
= json
!({"ps1": ""}
);
221 &NESTED_PROPERTY_SCHEMA
,
224 ("ps1/prop1", "property is missing and it is not optional"),
225 ("ps1/prop3", "property is missing and it is not optional"),
233 fn verify_all_of_schema() -> Result
<(), Error
> {
237 "another1": "another hello",
239 ALL_OF_SCHEMA_NO_ADDITIONAL
241 .expect("all of schema failed to verify valid object");
248 &ALL_OF_SCHEMA_NO_ADDITIONAL
,
250 &[("another1", "property is missing and it is not optional")],
256 "another1": "another hello",
257 "additional": "additional value",
260 &ALL_OF_SCHEMA_NO_ADDITIONAL
,
262 &[("additional", "schema does not allow additional properties")],
269 fn verify_all_of_schema_with_additional() -> Result
<(), Error
> {
273 "regular1": "another hello",
274 "more": "additional property",
276 ALL_OF_SCHEMA_ADDITIONAL
278 .expect("all of schema failed to verify valid object");
283 "more": "additional property",
286 &ALL_OF_SCHEMA_ADDITIONAL
,
288 &[("regular1", "property is missing and it is not optional")],