Debian Codegraph @

src/keywords/mod.rs

raw symbols SCIP
1 pub(crate) mod additional_items;
2 pub(crate) mod additional_properties;
3 pub(crate) mod all_of;
4 pub(crate) mod any_of;
5 pub(crate) mod boolean;
6 pub(crate) mod const_;
7 pub(crate) mod contains;
8 pub(crate) mod content;
9 pub(crate) mod custom;
10 pub(crate) mod dependencies;
11 pub(crate) mod enum_;
12 pub(crate) mod format;
13 pub(crate) mod helpers;
14 pub(crate) mod if_;
15 pub(crate) mod items;
16 pub(crate) mod legacy;
17 pub(crate) mod max_items;
18 pub(crate) mod max_length;
19 pub(crate) mod max_properties;
20 pub(crate) mod min_items;
21 pub(crate) mod min_length;
22 pub(crate) mod min_properties;
23 pub(crate) mod minmax;
24 pub(crate) mod multiple_of;
25 pub(crate) mod not;
26 pub(crate) mod one_of;
27 pub(crate) mod pattern;
28 pub(crate) mod pattern_properties;
29 pub(crate) mod prefix_items;
30 pub(crate) mod properties;
31 pub(crate) mod property_names;
32 pub(crate) mod ref_;
33 pub(crate) mod required;
34 pub(crate) mod type_;
35 pub(crate) mod unevaluated_items;
36 pub(crate) mod unevaluated_properties;
37 pub(crate) mod unique_items;
38 use core::fmt;
39
40 use referencing::{Draft, Vocabulary};
41 use serde_json::{Map, Value};
42
43 use crate::{compiler, error, validator::Validate};
44
45 pub(crate) type CompilationResult<'a> = Result<BoxedValidator, error::ValidationError<'a>>;
46 pub(crate) type BoxedValidator = Box<dyn Validate>;
47
48 type CompileFunc<'a> =
49 fn(&'a compiler::Context, &'a Map<String, Value>, &'a Value) -> Option<CompilationResult<'a>>;
50
51 #[derive(Debug, Clone)]
52 pub(crate) enum Keyword {
53 Builtin(BuiltinKeyword),
54 Custom(Box<str>),
55 }
56
57 #[derive(Debug, Clone)]
58 pub(crate) enum BuiltinKeyword {
59 Ref,
60 AdditionalItems,
61 AdditionalProperties,
62 AllOf,
63 AnyOf,
64 Dependencies,
65 Enum,
66 Format,
67 Items,
68 MaxItems,
69 MaxLength,
70 MaxProperties,
71 MinItems,
72 MinLength,
73 MinProperties,
74 MultipleOf,
75 Not,
76 OneOf,
77 Pattern,
78 PatternProperties,
79 Properties,
80 Required,
81 UniqueItems,
82 Maximum,
83 Minimum,
84 Type,
85 Const,
86 Contains,
87 ExclusiveMaximum,
88 ExclusiveMinimum,
89 PropertyNames,
90 ContentMediaType,
91 ContentEncoding,
92 If,
93 RecursiveRef,
94 DependentRequired,
95 DependentSchemas,
96 PrefixItems,
97 UnevaluatedItems,
98 UnevaluatedProperties,
99 DynamicRef,
100 }
101
102 impl BuiltinKeyword {
103 pub(crate) fn as_str(&self) -> &'static str {
104 match self {
105 Self::Ref => "$ref",
106 Self::AdditionalItems => "additionalItems",
107 Self::AdditionalProperties => "additionalProperties",
108 Self::AllOf => "allOf",
109 Self::AnyOf => "anyOf",
110 Self::Dependencies => "dependencies",
111 Self::Enum => "enum",
112 Self::Format => "format",
113 Self::Items => "items",
114 Self::MaxItems => "maxItems",
115 Self::MaxLength => "maxLength",
116 Self::MaxProperties => "maxProperties",
117 Self::MinItems => "minItems",
118 Self::MinLength => "minLength",
119 Self::MinProperties => "minProperties",
120 Self::MultipleOf => "multipleOf",
121 Self::Not => "not",
122 Self::OneOf => "oneOf",
123 Self::Pattern => "pattern",
124 Self::PatternProperties => "patternProperties",
125 Self::Properties => "properties",
126 Self::Required => "required",
127 Self::UniqueItems => "uniqueItems",
128 Self::Maximum => "maximum",
129 Self::Minimum => "minimum",
130 Self::Type => "type",
131 Self::Const => "const",
132 Self::Contains => "contains",
133 Self::ExclusiveMaximum => "exclusiveMaximum",
134 Self::ExclusiveMinimum => "exclusiveMinimum",
135 Self::PropertyNames => "propertyNames",
136 Self::ContentMediaType => "contentMediaType",
137 Self::ContentEncoding => "contentEncoding",
138 Self::If => "if",
139 Self::RecursiveRef => "$recursiveRef",
140 Self::DependentRequired => "dependentRequired",
141 Self::DependentSchemas => "dependentSchemas",
142 Self::PrefixItems => "prefixItems",
143 Self::UnevaluatedItems => "unevaluatedItems",
144 Self::UnevaluatedProperties => "unevaluatedProperties",
145 Self::DynamicRef => "$dynamicRef",
146 }
147 }
148 }
149
150 impl Keyword {
151 pub(crate) fn custom(name: impl Into<String>) -> Self {
152 Keyword::Custom(name.into().into_boxed_str())
153 }
154 pub(crate) fn as_str(&self) -> &str {
155 match self {
156 Self::Builtin(d) => d.as_str(),
157 Self::Custom(s) => s,
158 }
159 }
160 }
161
162 impl From<BuiltinKeyword> for Keyword {
163 fn from(value: BuiltinKeyword) -> Self {
164 Keyword::Builtin(value)
165 }
166 }
167
168 impl fmt::Display for Keyword {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 f.write_str(self.as_str())
171 }
172 }
173
174 pub(crate) fn get_for_draft<'a>(
175 ctx: &compiler::Context<'a>,
176 keyword: &'a str,
177 ) -> Option<(Keyword, CompileFunc<'a>)> {
178 match (ctx.draft(), keyword) {
179 // Keywords common to all drafts
180 (_, "$ref") => Some((BuiltinKeyword::Ref.into(), ref_::compile_ref)),
181 (_, "additionalItems") if ctx.has_vocabulary(&Vocabulary::Applicator) => Some((
182 BuiltinKeyword::AdditionalItems.into(),
183 additional_items::compile,
184 )),
185 (_, "additionalProperties") if ctx.has_vocabulary(&Vocabulary::Applicator) => Some((
186 BuiltinKeyword::AdditionalProperties.into(),
187 additional_properties::compile,
188 )),
189 (_, "allOf") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
190 Some((BuiltinKeyword::AllOf.into(), all_of::compile))
191 }
192 (_, "anyOf") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
193 Some((BuiltinKeyword::AnyOf.into(), any_of::compile))
194 }
195 (_, "dependencies") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
196 Some((BuiltinKeyword::Dependencies.into(), dependencies::compile))
197 }
198 (_, "enum") if ctx.has_vocabulary(&Vocabulary::Validation) => {
199 Some((BuiltinKeyword::Enum.into(), enum_::compile))
200 }
201 (Draft::Draft201909, "format") if ctx.has_vocabulary(&Vocabulary::Format) => {
202 Some((BuiltinKeyword::Format.into(), format::compile))
203 }
204 (Draft::Draft202012 | Draft::Unknown, "format")
205 if ctx.has_vocabulary(&Vocabulary::FormatAnnotation) =>
206 {
207 Some((BuiltinKeyword::Format.into(), format::compile))
208 }
209 (_, "format") => Some((BuiltinKeyword::Format.into(), format::compile)),
210 (_, "items") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
211 Some((BuiltinKeyword::Items.into(), items::compile))
212 }
213 (_, "maxItems") if ctx.has_vocabulary(&Vocabulary::Validation) => {
214 Some((BuiltinKeyword::MaxItems.into(), max_items::compile))
215 }
216 (_, "maxLength") if ctx.has_vocabulary(&Vocabulary::Validation) => {
217 Some((BuiltinKeyword::MaxLength.into(), max_length::compile))
218 }
219 (_, "maxProperties") if ctx.has_vocabulary(&Vocabulary::Validation) => Some((
220 BuiltinKeyword::MaxProperties.into(),
221 max_properties::compile,
222 )),
223 (_, "minItems") if ctx.has_vocabulary(&Vocabulary::Validation) => {
224 Some((BuiltinKeyword::MinItems.into(), min_items::compile))
225 }
226 (_, "minLength") if ctx.has_vocabulary(&Vocabulary::Validation) => {
227 Some((BuiltinKeyword::MinLength.into(), min_length::compile))
228 }
229 (_, "minProperties") if ctx.has_vocabulary(&Vocabulary::Validation) => Some((
230 BuiltinKeyword::MinProperties.into(),
231 min_properties::compile,
232 )),
233 (_, "multipleOf") if ctx.has_vocabulary(&Vocabulary::Validation) => {
234 Some((BuiltinKeyword::MultipleOf.into(), multiple_of::compile))
235 }
236 (_, "not") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
237 Some((BuiltinKeyword::Not.into(), not::compile))
238 }
239 (_, "oneOf") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
240 Some((BuiltinKeyword::OneOf.into(), one_of::compile))
241 }
242 (_, "pattern") if ctx.has_vocabulary(&Vocabulary::Validation) => {
243 Some((BuiltinKeyword::Pattern.into(), pattern::compile))
244 }
245 (_, "patternProperties") if ctx.has_vocabulary(&Vocabulary::Applicator) => Some((
246 BuiltinKeyword::PatternProperties.into(),
247 pattern_properties::compile,
248 )),
249 (_, "properties") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
250 Some((BuiltinKeyword::Properties.into(), properties::compile))
251 }
252 (_, "required") if ctx.has_vocabulary(&Vocabulary::Validation) => {
253 Some((BuiltinKeyword::Required.into(), required::compile))
254 }
255 (_, "uniqueItems") if ctx.has_vocabulary(&Vocabulary::Validation) => {
256 Some((BuiltinKeyword::UniqueItems.into(), unique_items::compile))
257 }
258 // Draft 4 specific
259 (Draft::Draft4, "maximum") => Some((
260 BuiltinKeyword::Maximum.into(),
261 legacy::maximum_draft_4::compile,
262 )),
263 (Draft::Draft4, "minimum") => Some((
264 BuiltinKeyword::Minimum.into(),
265 legacy::minimum_draft_4::compile,
266 )),
267 (Draft::Draft4, "type") => {
268 Some((BuiltinKeyword::Type.into(), legacy::type_draft_4::compile))
269 }
270 // Draft 6 and later
271 (
272 Draft::Draft6
273 | Draft::Draft7
274 | Draft::Draft201909
275 | Draft::Draft202012
276 | Draft::Unknown,
277 "const",
278 ) if ctx.has_vocabulary(&Vocabulary::Validation) => {
279 Some((BuiltinKeyword::Const.into(), const_::compile))
280 }
281 (
282 Draft::Draft6
283 | Draft::Draft7
284 | Draft::Draft201909
285 | Draft::Draft202012
286 | Draft::Unknown,
287 "contains",
288 ) if ctx.has_vocabulary(&Vocabulary::Applicator) => {
289 Some((BuiltinKeyword::Contains.into(), contains::compile))
290 }
291 (
292 Draft::Draft6
293 | Draft::Draft7
294 | Draft::Draft201909
295 | Draft::Draft202012
296 | Draft::Unknown,
297 "exclusiveMaximum",
298 ) if ctx.has_vocabulary(&Vocabulary::Validation) => Some((
299 BuiltinKeyword::ExclusiveMaximum.into(),
300 minmax::compile_exclusive_maximum,
301 )),
302 (
303 Draft::Draft6
304 | Draft::Draft7
305 | Draft::Draft201909
306 | Draft::Draft202012
307 | Draft::Unknown,
308 "exclusiveMinimum",
309 ) if ctx.has_vocabulary(&Vocabulary::Validation) => Some((
310 BuiltinKeyword::ExclusiveMinimum.into(),
311 minmax::compile_exclusive_minimum,
312 )),
313 (
314 Draft::Draft6
315 | Draft::Draft7
316 | Draft::Draft201909
317 | Draft::Draft202012
318 | Draft::Unknown,
319 "maximum",
320 ) if ctx.has_vocabulary(&Vocabulary::Validation) => {
321 Some((BuiltinKeyword::Maximum.into(), minmax::compile_maximum))
322 }
323 (
324 Draft::Draft6
325 | Draft::Draft7
326 | Draft::Draft201909
327 | Draft::Draft202012
328 | Draft::Unknown,
329 "minimum",
330 ) if ctx.has_vocabulary(&Vocabulary::Validation) => {
331 Some((BuiltinKeyword::Minimum.into(), minmax::compile_minimum))
332 }
333 (
334 Draft::Draft6
335 | Draft::Draft7
336 | Draft::Draft201909
337 | Draft::Draft202012
338 | Draft::Unknown,
339 "propertyNames",
340 ) if ctx.has_vocabulary(&Vocabulary::Applicator) => Some((
341 BuiltinKeyword::PropertyNames.into(),
342 property_names::compile,
343 )),
344 (
345 Draft::Draft6
346 | Draft::Draft7
347 | Draft::Draft201909
348 | Draft::Draft202012
349 | Draft::Unknown,
350 "type",
351 ) if ctx.has_vocabulary(&Vocabulary::Validation) => {
352 Some((BuiltinKeyword::Type.into(), type_::compile))
353 }
354 (Draft::Draft6 | Draft::Draft7, "contentMediaType") => Some((
355 BuiltinKeyword::ContentMediaType.into(),
356 content::compile_media_type,
357 )),
358 (Draft::Draft6 | Draft::Draft7, "contentEncoding") => Some((
359 BuiltinKeyword::ContentEncoding.into(),
360 content::compile_content_encoding,
361 )),
362 (Draft::Draft7 | Draft::Draft201909 | Draft::Draft202012 | Draft::Unknown, "if")
363 if ctx.has_vocabulary(&Vocabulary::Applicator) =>
364 {
365 Some((BuiltinKeyword::If.into(), if_::compile))
366 }
367 // Draft 2019-09 specific
368 (Draft::Draft201909, "$recursiveRef") => Some((
369 BuiltinKeyword::RecursiveRef.into(),
370 ref_::compile_recursive_ref,
371 )),
372 (Draft::Draft201909 | Draft::Draft202012 | Draft::Unknown, "dependentRequired")
373 if ctx.has_vocabulary(&Vocabulary::Validation) =>
374 {
375 Some((
376 BuiltinKeyword::DependentRequired.into(),
377 dependencies::compile_dependent_required,
378 ))
379 }
380 (Draft::Draft201909 | Draft::Draft202012 | Draft::Unknown, "dependentSchemas")
381 if ctx.has_vocabulary(&Vocabulary::Applicator) =>
382 {
383 Some((
384 BuiltinKeyword::DependentSchemas.into(),
385 dependencies::compile_dependent_schemas,
386 ))
387 }
388 (Draft::Draft201909, "unevaluatedItems") if ctx.has_vocabulary(&Vocabulary::Applicator) => {
389 Some((
390 BuiltinKeyword::UnevaluatedItems.into(),
391 unevaluated_items::compile,
392 ))
393 }
394 (Draft::Draft202012 | Draft::Unknown, "unevaluatedItems")
395 if ctx.has_vocabulary(&Vocabulary::Unevaluated) =>
396 {
397 Some((
398 BuiltinKeyword::UnevaluatedItems.into(),
399 unevaluated_items::compile,
400 ))
401 }
402 (Draft::Draft201909, "unevaluatedProperties")
403 if ctx.has_vocabulary(&Vocabulary::Applicator) =>
404 {
405 Some((
406 BuiltinKeyword::UnevaluatedProperties.into(),
407 unevaluated_properties::compile,
408 ))
409 }
410 (Draft::Draft202012 | Draft::Unknown, "unevaluatedProperties")
411 if ctx.has_vocabulary(&Vocabulary::Unevaluated) =>
412 {
413 Some((
414 BuiltinKeyword::UnevaluatedProperties.into(),
415 unevaluated_properties::compile,
416 ))
417 }
418 // Draft 2020-12 specific
419 (Draft::Draft202012 | Draft::Unknown, "prefixItems")
420 if ctx.has_vocabulary(&Vocabulary::Applicator) =>
421 {
422 Some((BuiltinKeyword::PrefixItems.into(), prefix_items::compile))
423 }
424 (Draft::Draft202012 | Draft::Unknown, "$dynamicRef") => {
425 Some((BuiltinKeyword::DynamicRef.into(), ref_::compile_dynamic_ref))
426 }
427 // Unknown or not-yet-implemented keyword
428 _ => None,
429 }
430 }
431
432 #[cfg(test)]
433 mod tests {
434 use serde_json::{json, Value};
435 use test_case::test_case;
436
437 #[test_case(&json!({"prefixItems": [{}], "items": {"type": "integer"}}), &json!([ null, 2, 3, "foo" ]), r#""foo" is not of type "integer""#)]
438 #[test_case(&json!({"prefixItems": [{}, {}, {}], "items": false}), &json!([ 1, 2, 3, 4 ]), r"False schema does not allow 4")]
439 #[test_case(&json!({"prefixItems": [{}, {}, {}], "items": false}), &json!([ 1, 2, 3, 4, 5 ]), r"False schema does not allow 4")]
440 #[test_case(&json!({"properties": {"foo": {}, "bar": {}}, "patternProperties": { "^v": {} }, "additionalProperties": false}), &json!({"foo" : 1, "bar" : 2, "quux" : "boom"}), r"Additional properties are not allowed ('quux' was unexpected)")]
441 #[test_case(&json!({"anyOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(1.5), r"1.5 is not valid under any of the schemas listed in the 'anyOf' keyword")]
442 #[test_case(&json!({"const": 2}), &json!(5), r"2 was expected")]
443 #[test_case(&json!({"contains": {"minimum": 5}}), &json!([2, 3, 4]), r"None of [2,3,4] are valid under the given schema")]
444 #[test_case(&json!({"enum": [1]}), &json!(4), r"4 is not one of 1")]
445 #[test_case(&json!({"enum": [1, 2]}), &json!(4), r"4 is not one of 1 or 2")]
446 #[test_case(&json!({"enum": [1, 2, 3]}), &json!(4), r"4 is not one of 1, 2 or 3")]
447 #[test_case(&json!({"enum": [1, 2, 3, 4]}), &json!(5), r"5 is not one of 1, 2 or 2 other candidates")]
448 #[test_case(&json!({"enum": [1, 2, 3, 4, 5]}), &json!(6), r"6 is not one of 1, 2 or 3 other candidates")]
449 #[test_case(&json!({"exclusiveMaximum": 3}), &json!(3.0), r"3.0 is greater than or equal to the maximum of 3")]
450 #[test_case(&json!({"exclusiveMaximum": 3.0}), &json!(3.0), r"3.0 is greater than or equal to the maximum of 3.0")]
451 #[test_case(&json!({"exclusiveMinimum": 1}), &json!(1.0), r"1.0 is less than or equal to the minimum of 1")]
452 #[test_case(&json!({"exclusiveMinimum": 1.0}), &json!(1), r"1 is less than or equal to the minimum of 1.0")]
453 #[test_case(&json!({"format": "ipv4"}), &json!("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), r#""2001:0db8:85a3:0000:0000:8a2e:0370:7334" is not a "ipv4""#)]
454 #[test_case(&json!({"maximum": 3}), &json!(3.5), r"3.5 is greater than the maximum of 3")]
455 #[test_case(&json!({"maximum": 3.0}), &json!(3.5), r"3.5 is greater than the maximum of 3.0")]
456 #[test_case(&json!({"minimum": 3}), &json!(2.5), r"2.5 is less than the minimum of 3")]
457 #[test_case(&json!({"minimum": 3.0}), &json!(2.5), r"2.5 is less than the minimum of 3.0")]
458 #[test_case(&json!({"maxItems": 2}), &json!([1, 2, 3]), r"[1,2,3] has more than 2 items")]
459 #[test_case(&json!({"maxLength": 2}), &json!("foo"), r#""foo" is longer than 2 characters"#)]
460 #[test_case(&json!({"maxProperties": 2}), &json!({"foo": 1, "bar": 2, "baz": 3}), r#"{"bar":2,"baz":3,"foo":1} has more than 2 properties"#)]
461 #[test_case(&json!({"minimum": 1.1}), &json!(0.6), r"0.6 is less than the minimum of 1.1")]
462 #[test_case(&json!({"minItems": 1}), &json!([]), r"[] has less than 1 item")]
463 #[test_case(&json!({"minLength": 2}), &json!("f"), r#""f" is shorter than 2 characters"#)]
464 #[test_case(&json!({"minProperties": 1}), &json!({}), r"{} has less than 1 property")]
465 #[test_case(&json!({"multipleOf": 2}), &json!(7), r"7 is not a multiple of 2")]
466 #[test_case(&json!({"not": {"type": "integer"}}), &json!(1), r#"{"type":"integer"} is not allowed for 1"#)]
467 #[test_case(&json!({"oneOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(1.1), r"1.1 is not valid under any of the schemas listed in the 'oneOf' keyword")]
468 #[test_case(&json!({"oneOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(3), r"3 is valid under more than one of the schemas listed in the 'oneOf' keyword")]
469 #[test_case(&json!({"pattern": "^a*$"}), &json!("abc"), r#""abc" does not match "^a*$""#)]
470 #[test_case(&json!({"properties": {"foo": {}, "bar": {}}, "required": ["foo"]}), &json!({"bar": 1}), r#""foo" is a required property"#)]
471 #[test_case(&json!({"type": "integer"}), &json!(1.1), r#"1.1 is not of type "integer""#)]
472 #[test_case(&json!({"type": ["integer", "string"]}), &json!(null), r#"null is not of types "integer", "string""#)]
473 #[test_case(&json!({"uniqueItems": true}), &json!([1, 1]), r"[1,1] has non-unique elements")]
474 fn error_message(schema: &Value, instance: &Value, expected: &str) {
475 let validator = crate::options()
476 .should_validate_formats(true)
477 .build(schema)
478 .expect("Invalid schema");
479 let errors: Vec<_> = validator.iter_errors(instance).collect();
480 assert_eq!(errors[0].to_string(), expected);
481 }
482
483 // Extra cases not covered by JSON test suite
484 #[test_case(&json!({"additionalProperties": {"type": "string"}}))]
485 #[test_case(&json!({"additionalProperties": {"type": "string"}, "properties": {"foo": {}}}))]
486 #[test_case(&json!({"additionalProperties": {"type": "string"}, "patternProperties": {"f.*o": {"type": "integer"}}}))]
487 #[test_case(&json!({"additionalProperties": {"type": "string"}, "properties": {"foo": {}}, "patternProperties": {"f.*o": {"type": "integer"}}}))]
488 #[test_case(&json!({"additionalProperties": false}))]
489 #[test_case(&json!({"additionalProperties": false, "properties": {"foo": {}}}))]
490 #[test_case(&json!({"additionalProperties": false, "patternProperties": {"f.*o": {"type": "integer"}}}))]
491 #[test_case(&json!({"additionalProperties": false, "properties": {"foo": {}}, "patternProperties": {"f.*o": {"type": "integer"}}}))]
492 #[test_case(&json!({"additionalItems": false, "prefixItems": [{"type": "string"}]}))]
493 #[test_case(&json!({"additionalItems": {"type": "integer"}, "prefixItems": [{"type": "string"}]}))]
494 #[test_case(&json!({"contains": {"minimum": 5}}))]
495 #[test_case(&json!({"contentMediaType": "application/json"}))]
496 #[test_case(&json!({"contentEncoding": "base64"}))]
497 #[test_case(&json!({"contentEncoding": "base64", "contentMediaType": "application/json"}))]
498 #[test_case(&json!({"dependencies": {"bar": ["foo"]}}))]
499 #[test_case(&json!({"exclusiveMaximum": 5}))]
500 #[test_case(&json!({"exclusiveMinimum": 5}))]
501 #[test_case(&json!({"format": "ipv4"}))]
502 #[test_case(&json!({"maximum": 2}))]
503 #[test_case(&json!({"maxItems": 2}))]
504 #[test_case(&json!({"maxProperties": 2}))]
505 #[test_case(&json!({"minProperties": 2}))]
506 #[test_case(&json!({"multipleOf": 2.5}))]
507 #[test_case(&json!({"multipleOf": 2}))]
508 #[test_case(&json!({"required": ["a"]}))]
509 #[test_case(&json!({"pattern": "^a"}))]
510 #[test_case(&json!({"patternProperties": {"f.*o": {"type": "integer"}}}))]
511 #[test_case(&json!({"propertyNames": {"maxLength": 3}}))]
512 fn is_valid_another_type(schema: &Value) {
513 let instance = json!(null);
514 assert!(crate::options()
515 .should_validate_formats(true)
516 .build(schema)
517 .expect("Invalid schema")
518 .is_valid(&instance));
519 }
520 #[test_case(&json!({"additionalProperties": false}), &json!({}))]
521 #[test_case(&json!({"additionalItems": false, "items": true}), &json!([]))]
522 fn is_valid(schema: &Value, instance: &Value) {
523 assert!(crate::is_valid(schema, instance));
524 }
525
526 #[test_case(&json!({"type": "number"}), &json!(42))]
527 #[test_case(&json!({"type": ["number", "null"]}), &json!(42))]
528 fn integer_is_valid_number_multi_type(schema: &Value, instance: &Value) {
529 // See: GH-147
530 assert!(crate::is_valid(schema, instance));
531 }
532 // enum: Number
533 #[test_case(&json!({"enum": [0.0]}), &json!(0))]
534 // enum: Array
535 #[test_case(&json!({"enum": [[1.0]]}), &json!([1]))]
536 // enum: Object
537 #[test_case(&json!({"enum": [{"a": 1.0}]}), &json!({"a": 1}))]
538 // enum:: Object in Array
539 #[test_case(&json!({"enum": [[{"b": 1.0}]]}), &json!([{"b": 1}]))]
540 // enum:: Array in Object
541 #[test_case(&json!({"enum": [{"c": [1.0]}]}), &json!({"c": [1]}))]
542 // const: Number
543 #[test_case(&json!({"const": 0.0}), &json!(0))]
544 // const: Array
545 #[test_case(&json!({"const": [1.0]}), &json!([1]))]
546 // const: Object
547 #[test_case(&json!({"const": {"a": 1.0}}), &json!({"a": 1}))]
548 // const:: Object in Array
549 #[test_case(&json!({"const": [{"b": 1.0}]}), &json!([{"b": 1}]))]
550 // const:: Array in Object
551 #[test_case(&json!({"const": {"c": [1.0]}}), &json!({"c": [1]}))]
552 fn numeric_equivalence(schema: &Value, instance: &Value) {
553 // See: GH-149
554 assert!(crate::is_valid(schema, instance));
555 }
556
557 #[test]
558 fn required_all_properties() {
559 // See: GH-190
560 let schema = json!({"required": ["foo", "bar"]});
561 let instance = json!({});
562 let validator = crate::validator_for(&schema).unwrap();
563 let errors: Vec<_> = validator.iter_errors(&instance).collect();
564 assert_eq!(errors.len(), 2);
565 assert_eq!(errors[0].to_string(), r#""foo" is a required property"#);
566 assert_eq!(errors[1].to_string(), r#""bar" is a required property"#);
567 }
568 }