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 fn compare_error(expected
: &[(&str, &str)], err
: Error
) -> Result
<(), Error
> {
41 let err
= match err
.downcast_ref
::<ParameterError
>() {
43 None
=> bail
!("unable to downcast error: {}", err
),
46 let result
= (move || {
47 let errors
= err
.errors();
49 if errors
.len() != expected
.len() {
51 "error list has different length: {} != {}",
57 for i
in 0..expected
.len() {
58 if expected
[i
].0 != errors
[i
].0 {
60 "error {} path differs: '{}' != '{}'",
66 if expected
[i
].1 != errors
[i
].1.to_string() {
68 "error {} message differs: '{}' != '{}'",
80 println
!("GOT: {:?}", err
);
89 expected_errors
: &[(&str, &str)],
90 ) -> Result
<(), Error
> {
91 match schema
.verify_json(data
) {
92 Ok(_
) => bail
!("expected errors, but got Ok()"),
93 Err(err
) => compare_error(expected_errors
, err
)?
,
99 fn verify_simple_object() -> Result
<(), Error
> {
100 let simple_value
= json
!({"prop1": 1, "prop4": "abc"}
);
103 &SIMPLE_OBJECT_SCHEMA
,
106 ("prop1", "Expected string value."),
107 ("prop4", "schema does not allow additional properties."),
108 ("prop3", "property is missing and it is not optional."),
116 fn verify_nested_object1() -> Result
<(), Error
> {
117 let nested_value
= json
!({"prop1": 1, "prop4": "abc"}
);
120 &NESTED_OBJECT_SCHEMA
,
123 ("prop1", "Expected string value."),
124 ("prop4", "schema does not allow additional properties."),
125 ("arr1", "property is missing and it is not optional."),
126 ("obj1", "property is missing and it is not optional."),
134 fn verify_nested_object2() -> Result
<(), Error
> {
135 let nested_value
= json
!({"prop1": 1, "prop4": "abc", "obj1": {}
, "arr1": ["abc", 0]});
138 &NESTED_OBJECT_SCHEMA
,
141 ("arr1/[1]", "Expected string value."),
142 ("obj1/prop1", "property is missing and it is not optional."),
143 ("obj1/prop3", "property is missing and it is not optional."),
144 ("prop1", "Expected string value."),
145 ("prop4", "schema does not allow additional properties."),
153 fn verify_nested_property1() -> Result
<(), Error
> {
154 let value
= json
!({"ps1": "abc"}
);
157 &NESTED_PROPERTY_SCHEMA
,
161 "Value without key, but schema does not define a default key.",
169 fn verify_nested_property2() -> Result
<(), Error
> {
170 let value
= json
!({"ps1": "abc=1"}
);
173 &NESTED_PROPERTY_SCHEMA
,
175 &[("ps1/abc", "schema does not allow additional properties.")],
182 fn verify_nested_property3() -> Result
<(), Error
> {
183 let value
= json
!({"ps1": ""}
);
186 &NESTED_PROPERTY_SCHEMA
,
189 ("ps1/prop1", "parameter is missing and it is not optional."),
190 ("ps1/prop3", "parameter is missing and it is not optional."),