| 1 | //! # Description |
| 2 | //! This module contains various validators for the `additionalProperties` keyword. |
| 3 | //! |
| 4 | //! The goal here is to compute intersections with another keywords affecting properties validation: |
| 5 | //! - `properties` |
| 6 | //! - `patternProperties` |
| 7 | //! |
| 8 | //! Each valid combination of these keywords has a validator here. |
| 9 | use crate::{ |
| 10 | compiler, |
| 11 | error::{no_error, ErrorIterator, ValidationError}, |
| 12 | evaluation::{format_schema_location, Annotations, ErrorDescription, EvaluationNode}, |
| 13 | keywords::CompilationResult, |
| 14 | node::SchemaNode, |
| 15 | options::PatternEngineOptions, |
| 16 | paths::{LazyLocation, Location}, |
| 17 | properties::{ |
| 18 | are_properties_valid, compile_big_map, compile_dynamic_prop_map_validator, |
| 19 | compile_fancy_regex_patterns, compile_regex_patterns, compile_small_map, BigValidatorsMap, |
| 20 | PropertiesValidatorsMap, SmallValidatorsMap, |
| 21 | }, |
| 22 | regex::RegexEngine, |
| 23 | types::JsonType, |
| 24 | validator::{EvaluationResult, Validate, ValidationContext}, |
| 25 | }; |
| 26 | use referencing::Uri; |
| 27 | use serde_json::{Map, Value}; |
| 28 | use std::sync::Arc; |
| 29 | |
| 30 | /// # Schema example |
| 31 | /// |
| 32 | /// ```json |
| 33 | /// { |
| 34 | /// "additionalProperties": {"type": "integer"}, |
| 35 | /// } |
| 36 | /// ``` |
| 37 | /// |
| 38 | /// # Valid value |
| 39 | /// |
| 40 | /// ```json |
| 41 | /// { |
| 42 | /// "bar": 6 |
| 43 | /// } |
| 44 | /// ``` |
| 45 | pub(crate) struct AdditionalPropertiesValidator { |
| 46 | node: SchemaNode, |
| 47 | } |
| 48 | impl AdditionalPropertiesValidator { |
| 49 | #[inline] |
| 50 | pub(crate) fn compile<'a>(schema: &'a Value, ctx: &compiler::Context) -> CompilationResult<'a> { |
| 51 | let ctx = ctx.new_at_location("additionalProperties"); |
| 52 | Ok(Box::new(AdditionalPropertiesValidator { |
| 53 | node: compiler::compile(&ctx, ctx.as_resource_ref(schema))?, |
| 54 | })) |
| 55 | } |
| 56 | } |
| 57 | impl Validate for AdditionalPropertiesValidator { |
| 58 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 59 | if let Value::Object(item) = instance { |
| 60 | item.values().all(|i| self.node.is_valid(i, ctx)) |
| 61 | } else { |
| 62 | true |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn validate<'i>( |
| 67 | &self, |
| 68 | instance: &'i Value, |
| 69 | location: &LazyLocation, |
| 70 | ctx: &mut ValidationContext, |
| 71 | ) -> Result<(), ValidationError<'i>> { |
| 72 | if let Value::Object(item) = instance { |
| 73 | for (name, value) in item { |
| 74 | self.node.validate(value, &location.push(name), ctx)?; |
| 75 | } |
| 76 | } |
| 77 | Ok(()) |
| 78 | } |
| 79 | |
| 80 | fn iter_errors<'i>( |
| 81 | &self, |
| 82 | instance: &'i Value, |
| 83 | location: &LazyLocation, |
| 84 | ctx: &mut ValidationContext, |
| 85 | ) -> ErrorIterator<'i> { |
| 86 | if let Value::Object(item) = instance { |
| 87 | let mut errors = Vec::new(); |
| 88 | for (name, value) in item { |
| 89 | errors.extend( |
| 90 | self.node |
| 91 | .iter_errors(value, &location.push(name.as_str()), ctx), |
| 92 | ); |
| 93 | } |
| 94 | ErrorIterator::from_iterator(errors.into_iter()) |
| 95 | } else { |
| 96 | no_error() |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn evaluate( |
| 101 | &self, |
| 102 | instance: &Value, |
| 103 | location: &LazyLocation, |
| 104 | ctx: &mut ValidationContext, |
| 105 | ) -> EvaluationResult { |
| 106 | if let Value::Object(item) = instance { |
| 107 | let mut children = Vec::with_capacity(item.len()); |
| 108 | for (name, value) in item { |
| 109 | children.push(self.node.evaluate_instance( |
| 110 | value, |
| 111 | &location.push(name.as_str()), |
| 112 | ctx, |
| 113 | )); |
| 114 | } |
| 115 | let mut result = EvaluationResult::from_children(children); |
| 116 | let annotated_props = item |
| 117 | .keys() |
| 118 | .cloned() |
| 119 | .map(serde_json::Value::String) |
| 120 | .collect(); |
| 121 | result.annotate(Annotations::new(serde_json::Value::Array(annotated_props))); |
| 122 | result |
| 123 | } else { |
| 124 | EvaluationResult::valid_empty() |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// # Schema example |
| 130 | /// |
| 131 | /// ```json |
| 132 | /// { |
| 133 | /// "additionalProperties": false |
| 134 | /// } |
| 135 | /// ``` |
| 136 | /// |
| 137 | /// # Valid value |
| 138 | /// |
| 139 | /// ```json |
| 140 | /// {} |
| 141 | /// ``` |
| 142 | pub(crate) struct AdditionalPropertiesFalseValidator { |
| 143 | location: Location, |
| 144 | } |
| 145 | impl AdditionalPropertiesFalseValidator { |
| 146 | #[inline] |
| 147 | pub(crate) fn compile<'a>(location: Location) -> CompilationResult<'a> { |
| 148 | Ok(Box::new(AdditionalPropertiesFalseValidator { location })) |
| 149 | } |
| 150 | } |
| 151 | impl Validate for AdditionalPropertiesFalseValidator { |
| 152 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 153 | if let Value::Object(item) = instance { |
| 154 | item.iter().next().is_none() |
| 155 | } else { |
| 156 | true |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | fn validate<'i>( |
| 161 | &self, |
| 162 | instance: &'i Value, |
| 163 | location: &LazyLocation, |
| 164 | _ctx: &mut ValidationContext, |
| 165 | ) -> Result<(), ValidationError<'i>> { |
| 166 | if let Value::Object(item) = instance { |
| 167 | if let Some((_, value)) = item.iter().next() { |
| 168 | return Err(ValidationError::false_schema( |
| 169 | self.location.clone(), |
| 170 | location.into(), |
| 171 | value, |
| 172 | )); |
| 173 | } |
| 174 | } |
| 175 | Ok(()) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// # Schema example |
| 180 | /// |
| 181 | /// ```json |
| 182 | /// { |
| 183 | /// "additionalProperties": false, |
| 184 | /// "properties": { |
| 185 | /// "foo": {"type": "string"} |
| 186 | /// }, |
| 187 | /// } |
| 188 | /// ``` |
| 189 | /// |
| 190 | /// # Valid value |
| 191 | /// |
| 192 | /// ```json |
| 193 | /// { |
| 194 | /// "foo": "bar", |
| 195 | /// } |
| 196 | /// ``` |
| 197 | pub(crate) struct AdditionalPropertiesNotEmptyFalseValidator<M: PropertiesValidatorsMap> { |
| 198 | properties: M, |
| 199 | location: Location, |
| 200 | } |
| 201 | impl AdditionalPropertiesNotEmptyFalseValidator<SmallValidatorsMap> { |
| 202 | #[inline] |
| 203 | pub(crate) fn compile<'a>( |
| 204 | map: &'a Map<String, Value>, |
| 205 | ctx: &compiler::Context, |
| 206 | ) -> CompilationResult<'a> { |
| 207 | Ok(Box::new(AdditionalPropertiesNotEmptyFalseValidator { |
| 208 | properties: compile_small_map(ctx, map)?, |
| 209 | location: ctx.location().join("additionalProperties"), |
| 210 | })) |
| 211 | } |
| 212 | } |
| 213 | impl AdditionalPropertiesNotEmptyFalseValidator<BigValidatorsMap> { |
| 214 | #[inline] |
| 215 | pub(crate) fn compile<'a>( |
| 216 | map: &'a Map<String, Value>, |
| 217 | ctx: &compiler::Context, |
| 218 | ) -> CompilationResult<'a> { |
| 219 | Ok(Box::new(AdditionalPropertiesNotEmptyFalseValidator { |
| 220 | properties: compile_big_map(ctx, map)?, |
| 221 | location: ctx.location().join("additionalProperties"), |
| 222 | })) |
| 223 | } |
| 224 | } |
| 225 | impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyFalseValidator<M> { |
| 226 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 227 | if let Value::Object(props) = instance { |
| 228 | are_properties_valid(&self.properties, props, ctx, |_, _| false) |
| 229 | } else { |
| 230 | true |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | fn validate<'i>( |
| 235 | &self, |
| 236 | instance: &'i Value, |
| 237 | location: &LazyLocation, |
| 238 | ctx: &mut ValidationContext, |
| 239 | ) -> Result<(), ValidationError<'i>> { |
| 240 | if let Value::Object(item) = instance { |
| 241 | for (property, value) in item { |
| 242 | if let Some((name, node)) = self.properties.get_key_validator(property) { |
| 243 | node.validate(value, &location.push(name), ctx)?; |
| 244 | } else { |
| 245 | return Err(ValidationError::additional_properties( |
| 246 | self.location.clone(), |
| 247 | location.into(), |
| 248 | instance, |
| 249 | vec![property.clone()], |
| 250 | )); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | Ok(()) |
| 255 | } |
| 256 | |
| 257 | fn iter_errors<'i>( |
| 258 | &self, |
| 259 | instance: &'i Value, |
| 260 | location: &LazyLocation, |
| 261 | ctx: &mut ValidationContext, |
| 262 | ) -> ErrorIterator<'i> { |
| 263 | if let Value::Object(item) = instance { |
| 264 | let mut errors = vec![]; |
| 265 | let mut unexpected = vec![]; |
| 266 | for (property, value) in item { |
| 267 | if let Some((name, node)) = self.properties.get_key_validator(property) { |
| 268 | errors.extend(node.iter_errors(value, &location.push(name.as_str()), ctx)); |
| 269 | } else { |
| 270 | unexpected.push(property.clone()); |
| 271 | } |
| 272 | } |
| 273 | if !unexpected.is_empty() { |
| 274 | errors.push(ValidationError::additional_properties( |
| 275 | self.location.clone(), |
| 276 | location.into(), |
| 277 | instance, |
| 278 | unexpected, |
| 279 | )); |
| 280 | } |
| 281 | ErrorIterator::from_iterator(errors.into_iter()) |
| 282 | } else { |
| 283 | no_error() |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | fn evaluate( |
| 288 | &self, |
| 289 | instance: &Value, |
| 290 | location: &LazyLocation, |
| 291 | ctx: &mut ValidationContext, |
| 292 | ) -> EvaluationResult { |
| 293 | if let Value::Object(item) = instance { |
| 294 | let mut unexpected = Vec::with_capacity(item.len()); |
| 295 | let mut children = Vec::with_capacity(item.len()); |
| 296 | for (property, value) in item { |
| 297 | if let Some((_name, node)) = self.properties.get_key_validator(property) { |
| 298 | children.push(node.evaluate_instance( |
| 299 | value, |
| 300 | &location.push(property.as_str()), |
| 301 | ctx, |
| 302 | )); |
| 303 | } else { |
| 304 | unexpected.push(property.clone()); |
| 305 | } |
| 306 | } |
| 307 | let mut result = EvaluationResult::from_children(children); |
| 308 | if !unexpected.is_empty() { |
| 309 | result.mark_errored(ErrorDescription::from_validation_error( |
| 310 | &ValidationError::additional_properties( |
| 311 | self.location.clone(), |
| 312 | location.into(), |
| 313 | instance, |
| 314 | unexpected, |
| 315 | ), |
| 316 | )); |
| 317 | } |
| 318 | result |
| 319 | } else { |
| 320 | EvaluationResult::valid_empty() |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /// # Schema example |
| 326 | /// |
| 327 | /// ```json |
| 328 | /// { |
| 329 | /// "additionalProperties": {"type": "integer"}, |
| 330 | /// "properties": { |
| 331 | /// "foo": {"type": "string"} |
| 332 | /// } |
| 333 | /// } |
| 334 | /// ``` |
| 335 | /// |
| 336 | /// # Valid value |
| 337 | /// |
| 338 | /// ```json |
| 339 | /// { |
| 340 | /// "foo": "bar", |
| 341 | /// "bar": 6 |
| 342 | /// } |
| 343 | /// ``` |
| 344 | pub(crate) struct AdditionalPropertiesNotEmptyValidator<M: PropertiesValidatorsMap> { |
| 345 | node: SchemaNode, |
| 346 | properties: M, |
| 347 | } |
| 348 | impl AdditionalPropertiesNotEmptyValidator<SmallValidatorsMap> { |
| 349 | #[inline] |
| 350 | pub(crate) fn compile<'a>( |
| 351 | map: &'a Map<String, Value>, |
| 352 | ctx: &compiler::Context, |
| 353 | schema: &'a Value, |
| 354 | ) -> CompilationResult<'a> { |
| 355 | let kctx = ctx.new_at_location("additionalProperties"); |
| 356 | Ok(Box::new(AdditionalPropertiesNotEmptyValidator { |
| 357 | properties: compile_small_map(ctx, map)?, |
| 358 | node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?, |
| 359 | })) |
| 360 | } |
| 361 | } |
| 362 | impl AdditionalPropertiesNotEmptyValidator<BigValidatorsMap> { |
| 363 | #[inline] |
| 364 | pub(crate) fn compile<'a>( |
| 365 | map: &'a Map<String, Value>, |
| 366 | ctx: &compiler::Context, |
| 367 | schema: &'a Value, |
| 368 | ) -> CompilationResult<'a> { |
| 369 | let kctx = ctx.new_at_location("additionalProperties"); |
| 370 | Ok(Box::new(AdditionalPropertiesNotEmptyValidator { |
| 371 | properties: compile_big_map(ctx, map)?, |
| 372 | node: compiler::compile(&kctx, kctx.as_resource_ref(schema))?, |
| 373 | })) |
| 374 | } |
| 375 | } |
| 376 | impl<M: PropertiesValidatorsMap> Validate for AdditionalPropertiesNotEmptyValidator<M> { |
| 377 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 378 | if let Value::Object(props) = instance { |
| 379 | are_properties_valid(&self.properties, props, ctx, |instance, ctx| { |
| 380 | self.node.is_valid(instance, ctx) |
| 381 | }) |
| 382 | } else { |
| 383 | true |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | fn validate<'i>( |
| 388 | &self, |
| 389 | instance: &'i Value, |
| 390 | location: &LazyLocation, |
| 391 | ctx: &mut ValidationContext, |
| 392 | ) -> Result<(), ValidationError<'i>> { |
| 393 | if let Value::Object(props) = instance { |
| 394 | for (property, value) in props { |
| 395 | let property_location = location.push(property); |
| 396 | if let Some(validator) = self.properties.get_validator(property) { |
| 397 | validator.validate(value, &property_location, ctx)?; |
| 398 | } else { |
| 399 | self.node.validate(value, &property_location, ctx)?; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | Ok(()) |
| 404 | } |
| 405 | |
| 406 | fn iter_errors<'i>( |
| 407 | &self, |
| 408 | instance: &'i Value, |
| 409 | location: &LazyLocation, |
| 410 | ctx: &mut ValidationContext, |
| 411 | ) -> ErrorIterator<'i> { |
| 412 | if let Value::Object(map) = instance { |
| 413 | let mut errors = vec![]; |
| 414 | for (property, value) in map { |
| 415 | if let Some((name, property_validators)) = |
| 416 | self.properties.get_key_validator(property) |
| 417 | { |
| 418 | errors.extend(property_validators.iter_errors( |
| 419 | value, |
| 420 | &location.push(name.as_str()), |
| 421 | ctx, |
| 422 | )); |
| 423 | } else { |
| 424 | errors.extend(self.node.iter_errors( |
| 425 | value, |
| 426 | &location.push(property.as_str()), |
| 427 | ctx, |
| 428 | )); |
| 429 | } |
| 430 | } |
| 431 | ErrorIterator::from_iterator(errors.into_iter()) |
| 432 | } else { |
| 433 | no_error() |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | fn evaluate( |
| 438 | &self, |
| 439 | instance: &Value, |
| 440 | location: &LazyLocation, |
| 441 | ctx: &mut ValidationContext, |
| 442 | ) -> EvaluationResult { |
| 443 | if let Value::Object(map) = instance { |
| 444 | let mut matched_propnames = Vec::with_capacity(map.len()); |
| 445 | let mut children = Vec::with_capacity(map.len()); |
| 446 | for (property, value) in map { |
| 447 | let path = location.push(property.as_str()); |
| 448 | if let Some((_name, property_validators)) = |
| 449 | self.properties.get_key_validator(property) |
| 450 | { |
| 451 | children.push(property_validators.evaluate_instance(value, &path, ctx)); |
| 452 | } else { |
| 453 | children.push(self.node.evaluate_instance(value, &path, ctx)); |
| 454 | matched_propnames.push(property.clone()); |
| 455 | } |
| 456 | } |
| 457 | let mut result = EvaluationResult::from_children(children); |
| 458 | if !matched_propnames.is_empty() { |
| 459 | result.annotate(Annotations::new(Value::from(matched_propnames))); |
| 460 | } |
| 461 | result |
| 462 | } else { |
| 463 | EvaluationResult::valid_empty() |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /// # Schema example |
| 469 | /// |
| 470 | /// ```json |
| 471 | /// { |
| 472 | /// "additionalProperties": {"type": "integer"}, |
| 473 | /// "patternProperties": { |
| 474 | /// "^x-": {"type": "integer", "minimum": 5}, |
| 475 | /// "-x$": {"type": "integer", "maximum": 10} |
| 476 | /// } |
| 477 | /// } |
| 478 | /// ``` |
| 479 | /// |
| 480 | /// # Valid value |
| 481 | /// |
| 482 | /// ```json |
| 483 | /// { |
| 484 | /// "x-foo": 6, |
| 485 | /// "foo-x": 7, |
| 486 | /// "bar": 8 |
| 487 | /// } |
| 488 | /// ``` |
| 489 | pub(crate) struct AdditionalPropertiesWithPatternsValidator<R> { |
| 490 | node: SchemaNode, |
| 491 | patterns: Vec<(R, SchemaNode)>, |
| 492 | /// We need this because `compiler::compile` uses the additionalProperties keyword to compile |
| 493 | /// this validator. That means that the schema node which contains this validator has |
| 494 | /// "additionalProperties" as it's path. However, we need to produce annotations which have the |
| 495 | /// patternProperties keyword as their path so we store the paths here. |
| 496 | pattern_keyword_path: Location, |
| 497 | pattern_keyword_absolute_location: Option<Arc<Uri<String>>>, |
| 498 | } |
| 499 | |
| 500 | impl<R: RegexEngine> Validate for AdditionalPropertiesWithPatternsValidator<R> { |
| 501 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 502 | if let Value::Object(item) = instance { |
| 503 | for (property, value) in item { |
| 504 | let mut has_match = false; |
| 505 | for (re, node) in &self.patterns { |
| 506 | if re.is_match(property).unwrap_or(false) { |
| 507 | has_match = true; |
| 508 | if !node.is_valid(value, ctx) { |
| 509 | return false; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | if !has_match && !self.node.is_valid(value, ctx) { |
| 514 | return false; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | true |
| 519 | } |
| 520 | |
| 521 | fn validate<'i>( |
| 522 | &self, |
| 523 | instance: &'i Value, |
| 524 | location: &LazyLocation, |
| 525 | ctx: &mut ValidationContext, |
| 526 | ) -> Result<(), ValidationError<'i>> { |
| 527 | if let Value::Object(item) = instance { |
| 528 | for (property, value) in item { |
| 529 | let property_location = location.push(property); |
| 530 | let mut has_match = false; |
| 531 | for (re, node) in &self.patterns { |
| 532 | if re.is_match(property).unwrap_or(false) { |
| 533 | has_match = true; |
| 534 | node.validate(value, &property_location, ctx)?; |
| 535 | } |
| 536 | } |
| 537 | if !has_match { |
| 538 | self.node.validate(value, &property_location, ctx)?; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | Ok(()) |
| 543 | } |
| 544 | |
| 545 | fn iter_errors<'i>( |
| 546 | &self, |
| 547 | instance: &'i Value, |
| 548 | location: &LazyLocation, |
| 549 | ctx: &mut ValidationContext, |
| 550 | ) -> ErrorIterator<'i> { |
| 551 | if let Value::Object(item) = instance { |
| 552 | let mut errors = vec![]; |
| 553 | for (property, value) in item { |
| 554 | let mut has_match = false; |
| 555 | for (re, node) in &self.patterns { |
| 556 | if re.is_match(property).unwrap_or(false) { |
| 557 | has_match = true; |
| 558 | errors.extend(node.iter_errors( |
| 559 | value, |
| 560 | &location.push(property.as_str()), |
| 561 | ctx, |
| 562 | )); |
| 563 | } |
| 564 | } |
| 565 | if !has_match { |
| 566 | errors.extend(self.node.iter_errors( |
| 567 | value, |
| 568 | &location.push(property.as_str()), |
| 569 | ctx, |
| 570 | )); |
| 571 | } |
| 572 | } |
| 573 | ErrorIterator::from_iterator(errors.into_iter()) |
| 574 | } else { |
| 575 | no_error() |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | fn evaluate( |
| 580 | &self, |
| 581 | instance: &Value, |
| 582 | location: &LazyLocation, |
| 583 | ctx: &mut ValidationContext, |
| 584 | ) -> EvaluationResult { |
| 585 | if let Value::Object(item) = instance { |
| 586 | let mut pattern_matched_propnames = Vec::with_capacity(item.len()); |
| 587 | let mut additional_matched_propnames = Vec::with_capacity(item.len()); |
| 588 | let mut children = Vec::with_capacity(item.len()); |
| 589 | for (property, value) in item { |
| 590 | let path = location.push(property.as_str()); |
| 591 | let mut has_match = false; |
| 592 | for (pattern, node) in &self.patterns { |
| 593 | if pattern.is_match(property).unwrap_or(false) { |
| 594 | has_match = true; |
| 595 | pattern_matched_propnames.push(property.clone()); |
| 596 | children.push(node.evaluate_instance(value, &path, ctx)); |
| 597 | } |
| 598 | } |
| 599 | if !has_match { |
| 600 | additional_matched_propnames.push(property.clone()); |
| 601 | children.push(self.node.evaluate_instance(value, &path, ctx)); |
| 602 | } |
| 603 | } |
| 604 | if !pattern_matched_propnames.is_empty() { |
| 605 | let annotation = Annotations::new(Value::from(pattern_matched_propnames)); |
| 606 | let schema_location = format_schema_location( |
| 607 | &self.pattern_keyword_path, |
| 608 | self.pattern_keyword_absolute_location.as_ref(), |
| 609 | ); |
| 610 | children.push(EvaluationNode::valid( |
| 611 | self.pattern_keyword_path.clone(), |
| 612 | self.pattern_keyword_absolute_location.clone(), |
| 613 | schema_location, |
| 614 | location.into(), |
| 615 | Some(annotation), |
| 616 | Vec::new(), |
| 617 | )); |
| 618 | } |
| 619 | let mut result = EvaluationResult::from_children(children); |
| 620 | if !additional_matched_propnames.is_empty() { |
| 621 | result.annotate(Annotations::new(Value::from(additional_matched_propnames))); |
| 622 | } |
| 623 | result |
| 624 | } else { |
| 625 | EvaluationResult::valid_empty() |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /// # Schema example |
| 631 | /// |
| 632 | /// ```json |
| 633 | /// { |
| 634 | /// "additionalProperties": false, |
| 635 | /// "patternProperties": { |
| 636 | /// "^x-": {"type": "integer", "minimum": 5}, |
| 637 | /// "-x$": {"type": "integer", "maximum": 10} |
| 638 | /// } |
| 639 | /// } |
| 640 | /// ``` |
| 641 | /// |
| 642 | /// # Valid value |
| 643 | /// |
| 644 | /// ```json |
| 645 | /// { |
| 646 | /// "x-bar": 6, |
| 647 | /// "spam-x": 7, |
| 648 | /// "x-baz-x": 8, |
| 649 | /// } |
| 650 | /// ``` |
| 651 | pub(crate) struct AdditionalPropertiesWithPatternsFalseValidator<R> { |
| 652 | patterns: Vec<(R, SchemaNode)>, |
| 653 | location: Location, |
| 654 | pattern_keyword_path: Location, |
| 655 | pattern_keyword_absolute_location: Option<Arc<Uri<String>>>, |
| 656 | } |
| 657 | |
| 658 | impl<R: RegexEngine> Validate for AdditionalPropertiesWithPatternsFalseValidator<R> { |
| 659 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 660 | if let Value::Object(item) = instance { |
| 661 | for (property, value) in item { |
| 662 | let mut has_match = false; |
| 663 | for (re, node) in &self.patterns { |
| 664 | if re.is_match(property).unwrap_or(false) { |
| 665 | has_match = true; |
| 666 | if !node.is_valid(value, ctx) { |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | if !has_match { |
| 672 | return false; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | true |
| 677 | } |
| 678 | |
| 679 | fn validate<'i>( |
| 680 | &self, |
| 681 | instance: &'i Value, |
| 682 | location: &LazyLocation, |
| 683 | ctx: &mut ValidationContext, |
| 684 | ) -> Result<(), ValidationError<'i>> { |
| 685 | if let Value::Object(item) = instance { |
| 686 | for (property, value) in item { |
| 687 | let property_location = location.push(property); |
| 688 | let mut has_match = false; |
| 689 | for (re, node) in &self.patterns { |
| 690 | if re.is_match(property).unwrap_or(false) { |
| 691 | has_match = true; |
| 692 | node.validate(value, &property_location, ctx)?; |
| 693 | } |
| 694 | } |
| 695 | if !has_match { |
| 696 | return Err(ValidationError::additional_properties( |
| 697 | self.location.clone(), |
| 698 | location.into(), |
| 699 | instance, |
| 700 | vec![property.clone()], |
| 701 | )); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | Ok(()) |
| 706 | } |
| 707 | |
| 708 | fn iter_errors<'i>( |
| 709 | &self, |
| 710 | instance: &'i Value, |
| 711 | location: &LazyLocation, |
| 712 | ctx: &mut ValidationContext, |
| 713 | ) -> ErrorIterator<'i> { |
| 714 | if let Value::Object(item) = instance { |
| 715 | let mut errors = vec![]; |
| 716 | let mut unexpected = vec![]; |
| 717 | for (property, value) in item { |
| 718 | let mut has_match = false; |
| 719 | for (re, node) in &self.patterns { |
| 720 | if re.is_match(property).unwrap_or(false) { |
| 721 | has_match = true; |
| 722 | errors.extend(node.iter_errors( |
| 723 | value, |
| 724 | &location.push(property.as_str()), |
| 725 | ctx, |
| 726 | )); |
| 727 | } |
| 728 | } |
| 729 | if !has_match { |
| 730 | unexpected.push(property.clone()); |
| 731 | } |
| 732 | } |
| 733 | if !unexpected.is_empty() { |
| 734 | errors.push(ValidationError::additional_properties( |
| 735 | self.location.clone(), |
| 736 | location.into(), |
| 737 | instance, |
| 738 | unexpected, |
| 739 | )); |
| 740 | } |
| 741 | ErrorIterator::from_iterator(errors.into_iter()) |
| 742 | } else { |
| 743 | no_error() |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | fn evaluate( |
| 748 | &self, |
| 749 | instance: &Value, |
| 750 | location: &LazyLocation, |
| 751 | ctx: &mut ValidationContext, |
| 752 | ) -> EvaluationResult { |
| 753 | if let Value::Object(item) = instance { |
| 754 | let mut unexpected = Vec::with_capacity(item.len()); |
| 755 | let mut pattern_matched_props = Vec::with_capacity(item.len()); |
| 756 | let mut children = Vec::with_capacity(item.len()); |
| 757 | for (property, value) in item { |
| 758 | let path = location.push(property.as_str()); |
| 759 | let mut has_match = false; |
| 760 | for (pattern, node) in &self.patterns { |
| 761 | if pattern.is_match(property).unwrap_or(false) { |
| 762 | has_match = true; |
| 763 | pattern_matched_props.push(property.clone()); |
| 764 | children.push(node.evaluate_instance(value, &path, ctx)); |
| 765 | } |
| 766 | } |
| 767 | if !has_match { |
| 768 | unexpected.push(property.clone()); |
| 769 | } |
| 770 | } |
| 771 | if !pattern_matched_props.is_empty() { |
| 772 | let annotation = Annotations::new(Value::from(pattern_matched_props)); |
| 773 | let schema_location = format_schema_location( |
| 774 | &self.pattern_keyword_path, |
| 775 | self.pattern_keyword_absolute_location.as_ref(), |
| 776 | ); |
| 777 | children.push(EvaluationNode::valid( |
| 778 | self.pattern_keyword_path.clone(), |
| 779 | self.pattern_keyword_absolute_location.clone(), |
| 780 | schema_location, |
| 781 | location.into(), |
| 782 | Some(annotation), |
| 783 | Vec::new(), |
| 784 | )); |
| 785 | } |
| 786 | let mut result = EvaluationResult::from_children(children); |
| 787 | if !unexpected.is_empty() { |
| 788 | result.mark_errored(ErrorDescription::from_validation_error( |
| 789 | &ValidationError::additional_properties( |
| 790 | self.location.clone(), |
| 791 | location.into(), |
| 792 | instance, |
| 793 | unexpected, |
| 794 | ), |
| 795 | )); |
| 796 | } |
| 797 | result |
| 798 | } else { |
| 799 | EvaluationResult::valid_empty() |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /// # Schema example |
| 805 | /// |
| 806 | /// ```json |
| 807 | /// { |
| 808 | /// "additionalProperties": {"type": "integer"}, |
| 809 | /// "properties": { |
| 810 | /// "foo": {"type": "string"} |
| 811 | /// }, |
| 812 | /// "patternProperties": { |
| 813 | /// "^x-": {"type": "integer", "minimum": 5}, |
| 814 | /// "-x$": {"type": "integer", "maximum": 10} |
| 815 | /// } |
| 816 | /// } |
| 817 | /// ``` |
| 818 | /// |
| 819 | /// # Valid value |
| 820 | /// |
| 821 | /// ```json |
| 822 | /// { |
| 823 | /// "foo": "a", |
| 824 | /// "x-spam": 6, |
| 825 | /// "spam-x": 7, |
| 826 | /// "x-spam-x": 8, |
| 827 | /// "bar": 42 |
| 828 | /// } |
| 829 | /// ``` |
| 830 | pub(crate) struct AdditionalPropertiesWithPatternsNotEmptyValidator<M: PropertiesValidatorsMap, R> { |
| 831 | node: SchemaNode, |
| 832 | properties: M, |
| 833 | patterns: Vec<(R, SchemaNode)>, |
| 834 | } |
| 835 | |
| 836 | impl<M: PropertiesValidatorsMap, R: RegexEngine> Validate |
| 837 | for AdditionalPropertiesWithPatternsNotEmptyValidator<M, R> |
| 838 | { |
| 839 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 840 | if let Value::Object(item) = instance { |
| 841 | for (property, value) in item { |
| 842 | if let Some(node) = self.properties.get_validator(property) { |
| 843 | if node.is_valid(value, ctx) { |
| 844 | for (re, pattern_node) in &self.patterns { |
| 845 | if re.is_match(property).unwrap_or(false) |
| 846 | && !pattern_node.is_valid(value, ctx) |
| 847 | { |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | } else { |
| 852 | return false; |
| 853 | } |
| 854 | } else { |
| 855 | let mut has_match = false; |
| 856 | for (re, node) in &self.patterns { |
| 857 | if re.is_match(property).unwrap_or(false) { |
| 858 | has_match = true; |
| 859 | if !node.is_valid(value, ctx) { |
| 860 | return false; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | if !has_match && !self.node.is_valid(value, ctx) { |
| 865 | return false; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | true |
| 870 | } else { |
| 871 | true |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | fn validate<'i>( |
| 876 | &self, |
| 877 | instance: &'i Value, |
| 878 | location: &LazyLocation, |
| 879 | ctx: &mut ValidationContext, |
| 880 | ) -> Result<(), ValidationError<'i>> { |
| 881 | if let Value::Object(item) = instance { |
| 882 | for (property, value) in item { |
| 883 | if let Some((name, node)) = self.properties.get_key_validator(property) { |
| 884 | let name_location = location.push(name); |
| 885 | node.validate(value, &name_location, ctx)?; |
| 886 | for (re, pattern_node) in &self.patterns { |
| 887 | if re.is_match(property).unwrap_or(false) { |
| 888 | pattern_node.validate(value, &name_location, ctx)?; |
| 889 | } |
| 890 | } |
| 891 | } else { |
| 892 | let property_location = location.push(property); |
| 893 | let mut has_match = false; |
| 894 | for (re, node) in &self.patterns { |
| 895 | if re.is_match(property).unwrap_or(false) { |
| 896 | has_match = true; |
| 897 | node.validate(value, &property_location, ctx)?; |
| 898 | } |
| 899 | } |
| 900 | if !has_match { |
| 901 | self.node.validate(value, &property_location, ctx)?; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | Ok(()) |
| 907 | } |
| 908 | |
| 909 | fn iter_errors<'i>( |
| 910 | &self, |
| 911 | instance: &'i Value, |
| 912 | location: &LazyLocation, |
| 913 | ctx: &mut ValidationContext, |
| 914 | ) -> ErrorIterator<'i> { |
| 915 | if let Value::Object(item) = instance { |
| 916 | let mut errors = vec![]; |
| 917 | for (property, value) in item { |
| 918 | if let Some((name, node)) = self.properties.get_key_validator(property) { |
| 919 | errors.extend(node.iter_errors(value, &location.push(name.as_str()), ctx)); |
| 920 | for (re, pattern_node) in &self.patterns { |
| 921 | if re.is_match(property).unwrap_or(false) { |
| 922 | errors.extend(pattern_node.iter_errors( |
| 923 | value, |
| 924 | &location.push(name.as_str()), |
| 925 | ctx, |
| 926 | )); |
| 927 | } |
| 928 | } |
| 929 | } else { |
| 930 | let mut has_match = false; |
| 931 | for (re, node) in &self.patterns { |
| 932 | if re.is_match(property).unwrap_or(false) { |
| 933 | has_match = true; |
| 934 | errors.extend(node.iter_errors( |
| 935 | value, |
| 936 | &location.push(property.as_str()), |
| 937 | ctx, |
| 938 | )); |
| 939 | } |
| 940 | } |
| 941 | if !has_match { |
| 942 | errors.extend(self.node.iter_errors( |
| 943 | value, |
| 944 | &location.push(property.as_str()), |
| 945 | ctx, |
| 946 | )); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | ErrorIterator::from_iterator(errors.into_iter()) |
| 951 | } else { |
| 952 | no_error() |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | fn evaluate( |
| 957 | &self, |
| 958 | instance: &Value, |
| 959 | location: &LazyLocation, |
| 960 | ctx: &mut ValidationContext, |
| 961 | ) -> EvaluationResult { |
| 962 | if let Value::Object(item) = instance { |
| 963 | let mut additional_matches = Vec::with_capacity(item.len()); |
| 964 | let mut children = Vec::with_capacity(item.len()); |
| 965 | for (property, value) in item { |
| 966 | let path = location.push(property.as_str()); |
| 967 | if let Some((_name, node)) = self.properties.get_key_validator(property) { |
| 968 | children.push(node.evaluate_instance(value, &path, ctx)); |
| 969 | for (pattern, pattern_node) in &self.patterns { |
| 970 | if pattern.is_match(property).unwrap_or(false) { |
| 971 | children.push(pattern_node.evaluate_instance(value, &path, ctx)); |
| 972 | } |
| 973 | } |
| 974 | } else { |
| 975 | let mut has_match = false; |
| 976 | for (pattern, node) in &self.patterns { |
| 977 | if pattern.is_match(property).unwrap_or(false) { |
| 978 | has_match = true; |
| 979 | children.push(node.evaluate_instance(value, &path, ctx)); |
| 980 | } |
| 981 | } |
| 982 | if !has_match { |
| 983 | additional_matches.push(property.clone()); |
| 984 | children.push(self.node.evaluate_instance(value, &path, ctx)); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | let mut result = EvaluationResult::from_children(children); |
| 989 | result.annotate(Annotations::new(Value::from(additional_matches))); |
| 990 | result |
| 991 | } else { |
| 992 | EvaluationResult::valid_empty() |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /// # Schema example |
| 998 | /// |
| 999 | /// ```json |
| 1000 | /// { |
| 1001 | /// "additionalProperties": false, |
| 1002 | /// "properties": { |
| 1003 | /// "foo": {"type": "string"} |
| 1004 | /// }, |
| 1005 | /// "patternProperties": { |
| 1006 | /// "^x-": {"type": "integer", "minimum": 5}, |
| 1007 | /// "-x$": {"type": "integer", "maximum": 10} |
| 1008 | /// } |
| 1009 | /// } |
| 1010 | /// ``` |
| 1011 | /// |
| 1012 | /// # Valid value |
| 1013 | /// |
| 1014 | /// ```json |
| 1015 | /// { |
| 1016 | /// "foo": "bar", |
| 1017 | /// "x-bar": 6, |
| 1018 | /// "spam-x": 7, |
| 1019 | /// "x-baz-x": 8, |
| 1020 | /// } |
| 1021 | /// ``` |
| 1022 | pub(crate) struct AdditionalPropertiesWithPatternsNotEmptyFalseValidator< |
| 1023 | M: PropertiesValidatorsMap, |
| 1024 | R, |
| 1025 | > { |
| 1026 | properties: M, |
| 1027 | patterns: Vec<(R, SchemaNode)>, |
| 1028 | location: Location, |
| 1029 | } |
| 1030 | |
| 1031 | impl<M: PropertiesValidatorsMap, R: RegexEngine> Validate |
| 1032 | for AdditionalPropertiesWithPatternsNotEmptyFalseValidator<M, R> |
| 1033 | { |
| 1034 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 1035 | if let Value::Object(item) = instance { |
| 1036 | for (property, value) in item { |
| 1037 | if let Some(node) = self.properties.get_validator(property) { |
| 1038 | if node.is_valid(value, ctx) { |
| 1039 | for (re, pattern_node) in &self.patterns { |
| 1040 | if re.is_match(property).unwrap_or(false) |
| 1041 | && !pattern_node.is_valid(value, ctx) |
| 1042 | { |
| 1043 | return false; |
| 1044 | } |
| 1045 | } |
| 1046 | } else { |
| 1047 | return false; |
| 1048 | } |
| 1049 | } else { |
| 1050 | let mut has_match = false; |
| 1051 | for (re, node) in &self.patterns { |
| 1052 | if re.is_match(property).unwrap_or(false) { |
| 1053 | has_match = true; |
| 1054 | if !node.is_valid(value, ctx) { |
| 1055 | return false; |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | if !has_match { |
| 1060 | return false; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | true |
| 1066 | } |
| 1067 | |
| 1068 | fn validate<'i>( |
| 1069 | &self, |
| 1070 | instance: &'i Value, |
| 1071 | location: &LazyLocation, |
| 1072 | ctx: &mut ValidationContext, |
| 1073 | ) -> Result<(), ValidationError<'i>> { |
| 1074 | if let Value::Object(item) = instance { |
| 1075 | for (property, value) in item { |
| 1076 | if let Some((name, node)) = self.properties.get_key_validator(property) { |
| 1077 | let name_location = location.push(name); |
| 1078 | node.validate(value, &name_location, ctx)?; |
| 1079 | for (re, pattern_node) in &self.patterns { |
| 1080 | if re.is_match(property).unwrap_or(false) { |
| 1081 | pattern_node.validate(value, &name_location, ctx)?; |
| 1082 | } |
| 1083 | } |
| 1084 | } else { |
| 1085 | let property_location = location.push(property); |
| 1086 | let mut has_match = false; |
| 1087 | for (re, node) in &self.patterns { |
| 1088 | if re.is_match(property).unwrap_or(false) { |
| 1089 | has_match = true; |
| 1090 | node.validate(value, &property_location, ctx)?; |
| 1091 | } |
| 1092 | } |
| 1093 | if !has_match { |
| 1094 | return Err(ValidationError::additional_properties( |
| 1095 | self.location.clone(), |
| 1096 | location.into(), |
| 1097 | instance, |
| 1098 | vec![property.clone()], |
| 1099 | )); |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | Ok(()) |
| 1105 | } |
| 1106 | |
| 1107 | fn iter_errors<'i>( |
| 1108 | &self, |
| 1109 | instance: &'i Value, |
| 1110 | location: &LazyLocation, |
| 1111 | ctx: &mut ValidationContext, |
| 1112 | ) -> ErrorIterator<'i> { |
| 1113 | if let Value::Object(item) = instance { |
| 1114 | let mut errors = vec![]; |
| 1115 | let mut unexpected = vec![]; |
| 1116 | for (property, value) in item { |
| 1117 | if let Some((name, node)) = self.properties.get_key_validator(property) { |
| 1118 | errors.extend(node.iter_errors(value, &location.push(name.as_str()), ctx)); |
| 1119 | for (re, pattern_node) in &self.patterns { |
| 1120 | if re.is_match(property).unwrap_or(false) { |
| 1121 | errors.extend(pattern_node.iter_errors( |
| 1122 | value, |
| 1123 | &location.push(name.as_str()), |
| 1124 | ctx, |
| 1125 | )); |
| 1126 | } |
| 1127 | } |
| 1128 | } else { |
| 1129 | let mut has_match = false; |
| 1130 | for (re, node) in &self.patterns { |
| 1131 | if re.is_match(property).unwrap_or(false) { |
| 1132 | has_match = true; |
| 1133 | errors.extend(node.iter_errors( |
| 1134 | value, |
| 1135 | &location.push(property.as_str()), |
| 1136 | ctx, |
| 1137 | )); |
| 1138 | } |
| 1139 | } |
| 1140 | if !has_match { |
| 1141 | unexpected.push(property.clone()); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | if !unexpected.is_empty() { |
| 1146 | errors.push(ValidationError::additional_properties( |
| 1147 | self.location.clone(), |
| 1148 | location.into(), |
| 1149 | instance, |
| 1150 | unexpected, |
| 1151 | )); |
| 1152 | } |
| 1153 | ErrorIterator::from_iterator(errors.into_iter()) |
| 1154 | } else { |
| 1155 | no_error() |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | fn evaluate( |
| 1160 | &self, |
| 1161 | instance: &Value, |
| 1162 | location: &LazyLocation, |
| 1163 | ctx: &mut ValidationContext, |
| 1164 | ) -> EvaluationResult { |
| 1165 | if let Value::Object(item) = instance { |
| 1166 | let mut unexpected = vec![]; |
| 1167 | let mut children = Vec::with_capacity(item.len()); |
| 1168 | for (property, value) in item { |
| 1169 | let path = location.push(property.as_str()); |
| 1170 | if let Some((_name, node)) = self.properties.get_key_validator(property) { |
| 1171 | children.push(node.evaluate_instance(value, &path, ctx)); |
| 1172 | for (pattern, pattern_node) in &self.patterns { |
| 1173 | if pattern.is_match(property).unwrap_or(false) { |
| 1174 | children.push(pattern_node.evaluate_instance(value, &path, ctx)); |
| 1175 | } |
| 1176 | } |
| 1177 | } else { |
| 1178 | let mut has_match = false; |
| 1179 | for (pattern, node) in &self.patterns { |
| 1180 | if pattern.is_match(property).unwrap_or(false) { |
| 1181 | has_match = true; |
| 1182 | children.push(node.evaluate_instance(value, &path, ctx)); |
| 1183 | } |
| 1184 | } |
| 1185 | if !has_match { |
| 1186 | unexpected.push(property.clone()); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | let mut result = EvaluationResult::from_children(children); |
| 1191 | if !unexpected.is_empty() { |
| 1192 | result.mark_errored(ErrorDescription::from_validation_error( |
| 1193 | &ValidationError::additional_properties( |
| 1194 | self.location.clone(), |
| 1195 | location.into(), |
| 1196 | instance, |
| 1197 | unexpected, |
| 1198 | ), |
| 1199 | )); |
| 1200 | } |
| 1201 | result |
| 1202 | } else { |
| 1203 | EvaluationResult::valid_empty() |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | macro_rules! try_compile { |
| 1209 | ($expr:expr) => { |
| 1210 | match $expr { |
| 1211 | Ok(result) => result, |
| 1212 | Err(error) => return Some(Err(error)), |
| 1213 | } |
| 1214 | }; |
| 1215 | } |
| 1216 | |
| 1217 | fn compile_pattern_non_empty<'a, R>( |
| 1218 | ctx: &compiler::Context, |
| 1219 | map: &'a Map<String, Value>, |
| 1220 | patterns: Vec<(R, SchemaNode)>, |
| 1221 | schema: &'a Value, |
| 1222 | ) -> Option<CompilationResult<'a>> |
| 1223 | where |
| 1224 | R: RegexEngine + 'static, |
| 1225 | { |
| 1226 | let kctx = ctx.new_at_location("additionalProperties"); |
| 1227 | if map.len() < 40 { |
| 1228 | Some(Ok(Box::new( |
| 1229 | AdditionalPropertiesWithPatternsNotEmptyValidator::<SmallValidatorsMap, R> { |
| 1230 | node: try_compile!(compiler::compile(&kctx, kctx.as_resource_ref(schema))), |
| 1231 | properties: try_compile!(compile_small_map(ctx, map)), |
| 1232 | patterns, |
| 1233 | }, |
| 1234 | ))) |
| 1235 | } else { |
| 1236 | Some(Ok(Box::new( |
| 1237 | AdditionalPropertiesWithPatternsNotEmptyValidator::<BigValidatorsMap, R> { |
| 1238 | node: try_compile!(compiler::compile(&kctx, kctx.as_resource_ref(schema))), |
| 1239 | properties: try_compile!(compile_big_map(ctx, map)), |
| 1240 | patterns, |
| 1241 | }, |
| 1242 | ))) |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | fn compile_pattern_non_empty_false<'a, R>( |
| 1247 | ctx: &compiler::Context, |
| 1248 | map: &'a Map<String, Value>, |
| 1249 | patterns: Vec<(R, SchemaNode)>, |
| 1250 | ) -> Option<CompilationResult<'a>> |
| 1251 | where |
| 1252 | R: RegexEngine + 'static, |
| 1253 | { |
| 1254 | let kctx = ctx.new_at_location("additionalProperties"); |
| 1255 | if map.len() < 40 { |
| 1256 | Some(Ok(Box::new( |
| 1257 | AdditionalPropertiesWithPatternsNotEmptyFalseValidator::<SmallValidatorsMap, R> { |
| 1258 | properties: try_compile!(compile_small_map(ctx, map)), |
| 1259 | patterns, |
| 1260 | location: kctx.location().clone(), |
| 1261 | }, |
| 1262 | ))) |
| 1263 | } else { |
| 1264 | Some(Ok(Box::new( |
| 1265 | AdditionalPropertiesWithPatternsNotEmptyFalseValidator::<BigValidatorsMap, R> { |
| 1266 | properties: try_compile!(compile_big_map(ctx, map)), |
| 1267 | patterns, |
| 1268 | location: kctx.location().clone(), |
| 1269 | }, |
| 1270 | ))) |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | #[inline] |
| 1275 | pub(crate) fn compile<'a>( |
| 1276 | ctx: &compiler::Context, |
| 1277 | parent: &'a Map<String, Value>, |
| 1278 | schema: &'a Value, |
| 1279 | ) -> Option<CompilationResult<'a>> { |
| 1280 | let properties = parent.get("properties"); |
| 1281 | if let Some(patterns) = parent.get("patternProperties") { |
| 1282 | if let Value::Object(obj) = patterns { |
| 1283 | // Compile all patterns & their validators to avoid doing work in the `patternProperties` validator |
| 1284 | match ctx.config().pattern_options() { |
| 1285 | PatternEngineOptions::FancyRegex { .. } => { |
| 1286 | let patterns = match compile_fancy_regex_patterns(ctx, obj) { |
| 1287 | Ok(patterns) => patterns, |
| 1288 | Err(error) => return Some(Err(error)), |
| 1289 | }; |
| 1290 | match schema { |
| 1291 | Value::Bool(true) => None, // "additionalProperties" are "true" by default |
| 1292 | Value::Bool(false) => { |
| 1293 | if let Some(properties) = properties { |
| 1294 | if let Value::Object(map) = properties { |
| 1295 | compile_pattern_non_empty_false::<fancy_regex::Regex>( |
| 1296 | ctx, map, patterns, |
| 1297 | ) |
| 1298 | } else { |
| 1299 | Some(Err(ValidationError::custom( |
| 1300 | Location::new(), |
| 1301 | Location::new(), |
| 1302 | properties, |
| 1303 | "Unexpected type", |
| 1304 | ))) |
| 1305 | } |
| 1306 | } else { |
| 1307 | Some(Ok(Box::new( |
| 1308 | AdditionalPropertiesWithPatternsFalseValidator { |
| 1309 | patterns, |
| 1310 | location: ctx.location().join("additionalProperties"), |
| 1311 | pattern_keyword_path: ctx |
| 1312 | .location() |
| 1313 | .join("patternProperties"), |
| 1314 | pattern_keyword_absolute_location: ctx |
| 1315 | .new_at_location("patternProperties") |
| 1316 | .base_uri(), |
| 1317 | }, |
| 1318 | ))) |
| 1319 | } |
| 1320 | } |
| 1321 | _ => { |
| 1322 | if let Some(properties) = properties { |
| 1323 | if let Value::Object(map) = properties { |
| 1324 | compile_pattern_non_empty::<fancy_regex::Regex>( |
| 1325 | ctx, map, patterns, schema, |
| 1326 | ) |
| 1327 | } else { |
| 1328 | Some(Err(ValidationError::custom( |
| 1329 | Location::new(), |
| 1330 | Location::new(), |
| 1331 | properties, |
| 1332 | "Unexpected type", |
| 1333 | ))) |
| 1334 | } |
| 1335 | } else { |
| 1336 | let kctx = ctx.new_at_location("additionalProperties"); |
| 1337 | Some(Ok(Box::new(AdditionalPropertiesWithPatternsValidator { |
| 1338 | node: try_compile!(compiler::compile( |
| 1339 | &kctx, |
| 1340 | kctx.as_resource_ref(schema), |
| 1341 | )), |
| 1342 | patterns, |
| 1343 | pattern_keyword_path: ctx.location().join("patternProperties"), |
| 1344 | pattern_keyword_absolute_location: ctx |
| 1345 | .new_at_location("patternProperties") |
| 1346 | .base_uri(), |
| 1347 | }))) |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | PatternEngineOptions::Regex { .. } => { |
| 1353 | let patterns = match compile_regex_patterns(ctx, obj) { |
| 1354 | Ok(patterns) => patterns, |
| 1355 | Err(error) => return Some(Err(error)), |
| 1356 | }; |
| 1357 | match schema { |
| 1358 | Value::Bool(true) => None, // "additionalProperties" are "true" by default |
| 1359 | Value::Bool(false) => { |
| 1360 | if let Some(properties) = properties { |
| 1361 | if let Value::Object(map) = properties { |
| 1362 | compile_pattern_non_empty_false::<regex::Regex>( |
| 1363 | ctx, map, patterns, |
| 1364 | ) |
| 1365 | } else { |
| 1366 | Some(Err(ValidationError::custom( |
| 1367 | Location::new(), |
| 1368 | Location::new(), |
| 1369 | properties, |
| 1370 | "Unexpected type", |
| 1371 | ))) |
| 1372 | } |
| 1373 | } else { |
| 1374 | Some(Ok(Box::new( |
| 1375 | AdditionalPropertiesWithPatternsFalseValidator { |
| 1376 | patterns, |
| 1377 | location: ctx.location().join("additionalProperties"), |
| 1378 | pattern_keyword_path: ctx |
| 1379 | .location() |
| 1380 | .join("patternProperties"), |
| 1381 | pattern_keyword_absolute_location: ctx |
| 1382 | .new_at_location("patternProperties") |
| 1383 | .base_uri(), |
| 1384 | }, |
| 1385 | ))) |
| 1386 | } |
| 1387 | } |
| 1388 | _ => { |
| 1389 | if let Some(properties) = properties { |
| 1390 | if let Value::Object(map) = properties { |
| 1391 | compile_pattern_non_empty::<regex::Regex>( |
| 1392 | ctx, map, patterns, schema, |
| 1393 | ) |
| 1394 | } else { |
| 1395 | Some(Err(ValidationError::custom( |
| 1396 | Location::new(), |
| 1397 | Location::new(), |
| 1398 | properties, |
| 1399 | "Unexpected type", |
| 1400 | ))) |
| 1401 | } |
| 1402 | } else { |
| 1403 | let kctx = ctx.new_at_location("additionalProperties"); |
| 1404 | Some(Ok(Box::new(AdditionalPropertiesWithPatternsValidator { |
| 1405 | node: try_compile!(compiler::compile( |
| 1406 | &kctx, |
| 1407 | kctx.as_resource_ref(schema), |
| 1408 | )), |
| 1409 | patterns, |
| 1410 | pattern_keyword_path: ctx.location().join("patternProperties"), |
| 1411 | pattern_keyword_absolute_location: ctx |
| 1412 | .new_at_location("patternProperties") |
| 1413 | .base_uri(), |
| 1414 | }))) |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | } else { |
| 1421 | Some(Err(ValidationError::single_type_error( |
| 1422 | Location::new(), |
| 1423 | ctx.location().clone(), |
| 1424 | schema, |
| 1425 | JsonType::Object, |
| 1426 | ))) |
| 1427 | } |
| 1428 | } else { |
| 1429 | match schema { |
| 1430 | Value::Bool(true) => None, // "additionalProperties" are "true" by default |
| 1431 | Value::Bool(false) => { |
| 1432 | if let Some(properties) = properties { |
| 1433 | compile_dynamic_prop_map_validator!( |
| 1434 | AdditionalPropertiesNotEmptyFalseValidator, |
| 1435 | properties, |
| 1436 | ctx, |
| 1437 | ) |
| 1438 | } else { |
| 1439 | let location = ctx.location().join("additionalProperties"); |
| 1440 | Some(AdditionalPropertiesFalseValidator::compile(location)) |
| 1441 | } |
| 1442 | } |
| 1443 | _ => { |
| 1444 | if let Some(properties) = properties { |
| 1445 | compile_dynamic_prop_map_validator!( |
| 1446 | AdditionalPropertiesNotEmptyValidator, |
| 1447 | properties, |
| 1448 | ctx, |
| 1449 | schema, |
| 1450 | ) |
| 1451 | } else { |
| 1452 | Some(AdditionalPropertiesValidator::compile(schema, ctx)) |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | #[cfg(test)] |
| 1460 | mod tests { |
| 1461 | use crate::tests_util; |
| 1462 | use serde_json::{json, Value}; |
| 1463 | use test_case::test_case; |
| 1464 | |
| 1465 | fn schema_1() -> Value { |
| 1466 | // For `AdditionalPropertiesWithPatternsNotEmptyFalseValidator` |
| 1467 | json!({ |
| 1468 | "additionalProperties": false, |
| 1469 | "properties": { |
| 1470 | "foo": {"type": "string"}, |
| 1471 | "barbaz": {"type": "integer", "multipleOf": 3}, |
| 1472 | }, |
| 1473 | "patternProperties": { |
| 1474 | "^bar": {"type": "integer", "minimum": 5}, |
| 1475 | "spam$": {"type": "integer", "maximum": 10}, |
| 1476 | } |
| 1477 | }) |
| 1478 | } |
| 1479 | |
| 1480 | // Another type |
| 1481 | #[test_case(&json!([1]))] |
| 1482 | // The right type |
| 1483 | #[test_case(&json!({}))] |
| 1484 | // Match `properties.foo` |
| 1485 | #[test_case(&json!({"foo": "a"}))] |
| 1486 | // Match `properties.barbaz` & `patternProperties.^bar` |
| 1487 | #[test_case(&json!({"barbaz": 6}))] |
| 1488 | // Match `patternProperties.^bar` |
| 1489 | #[test_case(&json!({"bar": 6}))] |
| 1490 | // Match `patternProperties.spam$` |
| 1491 | #[test_case(&json!({"spam": 7}))] |
| 1492 | // All `patternProperties` rules match on different values |
| 1493 | #[test_case(&json!({"bar": 6, "spam": 7}))] |
| 1494 | // All `patternProperties` rules match on the same value |
| 1495 | #[test_case(&json!({"barspam": 7}))] |
| 1496 | // All combined |
| 1497 | #[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7, "foo": "a", "barbaz": 6}))] |
| 1498 | fn schema_1_valid(instance: &Value) { |
| 1499 | let schema = schema_1(); |
| 1500 | tests_util::is_valid(&schema, instance); |
| 1501 | } |
| 1502 | |
| 1503 | // `properties.foo` - should be a string |
| 1504 | #[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])] |
| 1505 | // `additionalProperties` - extra keyword & not in `properties` / `patternProperties` |
| 1506 | #[test_case(&json!({"faz": 1}), &["Additional properties are not allowed (\'faz\' was unexpected)"], &["/additionalProperties"])] |
| 1507 | #[test_case(&json!({"faz": 1, "haz": 1}), &["Additional properties are not allowed (\'faz\', \'haz\' were unexpected)"], &["/additionalProperties"])] |
| 1508 | // `properties.foo` - should be a string & `patternProperties.^bar` - invalid |
| 1509 | #[test_case(&json!({"foo": 3, "bar": 4}), &["4 is less than the minimum of 5", "3 is not of type \"string\""], &["/patternProperties/^bar/minimum", "/properties/foo/type"])] |
| 1510 | // `properties.barbaz` - valid; `patternProperties.^bar` - invalid |
| 1511 | #[test_case(&json!({"barbaz": 3}), &["3 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1512 | // `patternProperties.^bar` (should be >=5) |
| 1513 | #[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1514 | // `patternProperties.spam$` (should be <=10) |
| 1515 | #[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1516 | // `patternProperties` - both values are invalid |
| 1517 | #[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])] |
| 1518 | // `patternProperties` - `bar` is valid, `spam` is invalid |
| 1519 | #[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1520 | // `patternProperties` - `bar` is invalid, `spam` is valid |
| 1521 | #[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1522 | // `patternProperties.^bar` - (should be >=5), but valid for `patternProperties.spam$` |
| 1523 | #[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1524 | // `patternProperties.spam$` - (should be <=10), but valid for `patternProperties.^bar` |
| 1525 | #[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1526 | // All combined |
| 1527 | #[test_case( |
| 1528 | &json!({"bar": 4, "spam": 11, "foo": 3, "faz": 1}), |
| 1529 | &[ |
| 1530 | "4 is less than the minimum of 5", |
| 1531 | "3 is not of type \"string\"", |
| 1532 | "11 is greater than the maximum of 10", |
| 1533 | "Additional properties are not allowed (\'faz\' was unexpected)" |
| 1534 | ], |
| 1535 | &[ |
| 1536 | "/patternProperties/^bar/minimum", |
| 1537 | "/properties/foo/type", |
| 1538 | "/patternProperties/spam$/maximum", |
| 1539 | "/additionalProperties" |
| 1540 | ] |
| 1541 | )] |
| 1542 | fn schema_1_invalid(instance: &Value, expected: &[&str], locations: &[&str]) { |
| 1543 | let schema = schema_1(); |
| 1544 | tests_util::is_not_valid(&schema, instance); |
| 1545 | tests_util::expect_errors(&schema, instance, expected); |
| 1546 | tests_util::assert_locations(&schema, instance, locations); |
| 1547 | } |
| 1548 | |
| 1549 | fn schema_2() -> Value { |
| 1550 | // For `AdditionalPropertiesWithPatternsFalseValidator` |
| 1551 | json!({ |
| 1552 | "additionalProperties": false, |
| 1553 | "patternProperties": { |
| 1554 | "^bar": {"type": "integer", "minimum": 5}, |
| 1555 | "spam$": {"type": "integer", "maximum": 10}, |
| 1556 | } |
| 1557 | }) |
| 1558 | } |
| 1559 | |
| 1560 | // Another type |
| 1561 | #[test_case(&json!([1]))] |
| 1562 | // The right type |
| 1563 | #[test_case(&json!({}))] |
| 1564 | // Match `patternProperties.^bar` |
| 1565 | #[test_case(&json!({"bar": 6}))] |
| 1566 | // Match `patternProperties.spam$` |
| 1567 | #[test_case(&json!({"spam": 7}))] |
| 1568 | // All `patternProperties` rules match on different values |
| 1569 | #[test_case(&json!({"bar": 6, "spam": 7}))] |
| 1570 | // All `patternProperties` rules match on the same value |
| 1571 | #[test_case(&json!({"barspam": 7}))] |
| 1572 | // All combined |
| 1573 | #[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7}))] |
| 1574 | fn schema_2_valid(instance: &Value) { |
| 1575 | let schema = schema_2(); |
| 1576 | tests_util::is_valid(&schema, instance); |
| 1577 | } |
| 1578 | |
| 1579 | // `additionalProperties` - extra keyword & not in `patternProperties` |
| 1580 | #[test_case(&json!({"faz": "a"}), &["Additional properties are not allowed (\'faz\' was unexpected)"], &["/additionalProperties"])] |
| 1581 | // `patternProperties.^bar` (should be >=5) |
| 1582 | #[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1583 | // `patternProperties.spam$` (should be <=10) |
| 1584 | #[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1585 | // `patternProperties` - both values are invalid |
| 1586 | #[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])] |
| 1587 | // `patternProperties` - `bar` is valid, `spam` is invalid |
| 1588 | #[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1589 | // `patternProperties` - `bar` is invalid, `spam` is valid |
| 1590 | #[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1591 | // `patternProperties.^bar` - (should be >=5), but valid for `patternProperties.spam$` |
| 1592 | #[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1593 | // `patternProperties.spam$` - (should be <=10), but valid for `patternProperties.^bar` |
| 1594 | #[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1595 | // All combined |
| 1596 | #[test_case( |
| 1597 | &json!({"bar": 4, "spam": 11, "faz": 1}), |
| 1598 | &[ |
| 1599 | "4 is less than the minimum of 5", |
| 1600 | "11 is greater than the maximum of 10", |
| 1601 | "Additional properties are not allowed (\'faz\' was unexpected)" |
| 1602 | ], |
| 1603 | &[ |
| 1604 | "/patternProperties/^bar/minimum", |
| 1605 | "/patternProperties/spam$/maximum", |
| 1606 | "/additionalProperties" |
| 1607 | ] |
| 1608 | )] |
| 1609 | fn schema_2_invalid(instance: &Value, expected: &[&str], locations: &[&str]) { |
| 1610 | let schema = schema_2(); |
| 1611 | tests_util::is_not_valid(&schema, instance); |
| 1612 | tests_util::expect_errors(&schema, instance, expected); |
| 1613 | tests_util::assert_locations(&schema, instance, locations); |
| 1614 | } |
| 1615 | |
| 1616 | fn schema_3() -> Value { |
| 1617 | // For `AdditionalPropertiesNotEmptyFalseValidator` |
| 1618 | json!({ |
| 1619 | "additionalProperties": false, |
| 1620 | "properties": { |
| 1621 | "foo": {"type": "string"} |
| 1622 | } |
| 1623 | }) |
| 1624 | } |
| 1625 | |
| 1626 | // Another type |
| 1627 | #[test_case(&json!([1]))] |
| 1628 | // The right type |
| 1629 | #[test_case(&json!({}))] |
| 1630 | // Match `properties` |
| 1631 | #[test_case(&json!({"foo": "a"}))] |
| 1632 | fn schema_3_valid(instance: &Value) { |
| 1633 | let schema = schema_3(); |
| 1634 | tests_util::is_valid(&schema, instance); |
| 1635 | } |
| 1636 | |
| 1637 | // `properties` - should be a string |
| 1638 | #[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])] |
| 1639 | // `additionalProperties` - extra keyword & not in `properties` |
| 1640 | #[test_case(&json!({"faz": "a"}), &["Additional properties are not allowed (\'faz\' was unexpected)"], &["/additionalProperties"])] |
| 1641 | // All combined |
| 1642 | #[test_case( |
| 1643 | &json!( |
| 1644 | {"foo": 3, "faz": "a"}), |
| 1645 | &[ |
| 1646 | "3 is not of type \"string\"", |
| 1647 | "Additional properties are not allowed (\'faz\' was unexpected)", |
| 1648 | ], |
| 1649 | &[ |
| 1650 | "/properties/foo/type", |
| 1651 | "/additionalProperties", |
| 1652 | ] |
| 1653 | )] |
| 1654 | fn schema_3_invalid(instance: &Value, expected: &[&str], locations: &[&str]) { |
| 1655 | let schema = schema_3(); |
| 1656 | tests_util::is_not_valid(&schema, instance); |
| 1657 | tests_util::expect_errors(&schema, instance, expected); |
| 1658 | tests_util::assert_locations(&schema, instance, locations); |
| 1659 | } |
| 1660 | |
| 1661 | fn schema_4() -> Value { |
| 1662 | // For `AdditionalPropertiesNotEmptyValidator` |
| 1663 | json!({ |
| 1664 | "additionalProperties": {"type": "integer"}, |
| 1665 | "properties": { |
| 1666 | "foo": {"type": "string"} |
| 1667 | } |
| 1668 | }) |
| 1669 | } |
| 1670 | |
| 1671 | // Another type |
| 1672 | #[test_case(&json!([1]))] |
| 1673 | // The right type |
| 1674 | #[test_case(&json!({}))] |
| 1675 | // Match `properties` |
| 1676 | #[test_case(&json!({"foo": "a"}))] |
| 1677 | // Match `additionalProperties` |
| 1678 | #[test_case(&json!({"bar": 4}))] |
| 1679 | // All combined |
| 1680 | #[test_case(&json!({"foo": "a", "bar": 4}))] |
| 1681 | fn schema_4_valid(instance: &Value) { |
| 1682 | let schema = schema_4(); |
| 1683 | tests_util::is_valid(&schema, instance); |
| 1684 | } |
| 1685 | |
| 1686 | // `properties` - should be a string |
| 1687 | #[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])] |
| 1688 | // `additionalProperties` - should be an integer |
| 1689 | #[test_case(&json!({"bar": "a"}), &["\"a\" is not of type \"integer\""], &["/additionalProperties/type"])] |
| 1690 | // All combined |
| 1691 | #[test_case( |
| 1692 | &json!( |
| 1693 | {"foo": 3, "bar": "a"}), |
| 1694 | &[ |
| 1695 | "\"a\" is not of type \"integer\"", |
| 1696 | "3 is not of type \"string\"" |
| 1697 | ], |
| 1698 | &[ |
| 1699 | "/additionalProperties/type", |
| 1700 | "/properties/foo/type", |
| 1701 | ] |
| 1702 | )] |
| 1703 | fn schema_4_invalid(instance: &Value, expected: &[&str], locations: &[&str]) { |
| 1704 | let schema = schema_4(); |
| 1705 | tests_util::is_not_valid(&schema, instance); |
| 1706 | tests_util::expect_errors(&schema, instance, expected); |
| 1707 | tests_util::assert_locations(&schema, instance, locations); |
| 1708 | } |
| 1709 | |
| 1710 | fn schema_5() -> Value { |
| 1711 | // For `AdditionalPropertiesWithPatternsNotEmptyValidator` |
| 1712 | json!({ |
| 1713 | "additionalProperties": {"type": "integer"}, |
| 1714 | "properties": { |
| 1715 | "foo": {"type": "string"}, |
| 1716 | "barbaz": {"type": "integer", "multipleOf": 3}, |
| 1717 | }, |
| 1718 | "patternProperties": { |
| 1719 | "^bar": {"type": "integer", "minimum": 5}, |
| 1720 | "spam$": {"type": "integer", "maximum": 10}, |
| 1721 | } |
| 1722 | }) |
| 1723 | } |
| 1724 | |
| 1725 | // Another type |
| 1726 | #[test_case(&json!([1]))] |
| 1727 | // The right type |
| 1728 | #[test_case(&json!({}))] |
| 1729 | // Match `properties.foo` |
| 1730 | #[test_case(&json!({"foo": "a"}))] |
| 1731 | // Match `additionalProperties` |
| 1732 | #[test_case(&json!({"faz": 42}))] |
| 1733 | // Match `properties.barbaz` & `patternProperties.^bar` |
| 1734 | #[test_case(&json!({"barbaz": 6}))] |
| 1735 | // Match `patternProperties.^bar` |
| 1736 | #[test_case(&json!({"bar": 6}))] |
| 1737 | // Match `patternProperties.spam$` |
| 1738 | #[test_case(&json!({"spam": 7}))] |
| 1739 | // All `patternProperties` rules match on different values |
| 1740 | #[test_case(&json!({"bar": 6, "spam": 7}))] |
| 1741 | // All `patternProperties` rules match on the same value |
| 1742 | #[test_case(&json!({"barspam": 7}))] |
| 1743 | // All combined |
| 1744 | #[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7, "foo": "a", "barbaz": 6, "faz": 42}))] |
| 1745 | fn schema_5_valid(instance: &Value) { |
| 1746 | let schema = schema_5(); |
| 1747 | tests_util::is_valid(&schema, instance); |
| 1748 | } |
| 1749 | |
| 1750 | // `properties.bar` - should be a string |
| 1751 | #[test_case(&json!({"foo": 3}), &["3 is not of type \"string\""], &["/properties/foo/type"])] |
| 1752 | // `additionalProperties` - extra keyword that doesn't match `additionalProperties` |
| 1753 | #[test_case(&json!({"faz": "a"}), &["\"a\" is not of type \"integer\""], &["/additionalProperties/type"])] |
| 1754 | #[test_case(&json!({"faz": "a", "haz": "a"}), &["\"a\" is not of type \"integer\"", "\"a\" is not of type \"integer\""], &["/additionalProperties/type", "/additionalProperties/type"])] |
| 1755 | // `properties.foo` - should be a string & `patternProperties.^bar` - invalid |
| 1756 | #[test_case(&json!({"foo": 3, "bar": 4}), &["4 is less than the minimum of 5", "3 is not of type \"string\""], &["/patternProperties/^bar/minimum", "/properties/foo/type"])] |
| 1757 | // `properties.barbaz` - valid; `patternProperties.^bar` - invalid |
| 1758 | #[test_case(&json!({"barbaz": 3}), &["3 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1759 | // `patternProperties.^bar` (should be >=5) |
| 1760 | #[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1761 | // `patternProperties.spam$` (should be <=10) |
| 1762 | #[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1763 | // `patternProperties` - both values are invalid |
| 1764 | #[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])] |
| 1765 | // `patternProperties` - `bar` is valid, `spam` is invalid |
| 1766 | #[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1767 | // `patternProperties` - `bar` is invalid, `spam` is valid |
| 1768 | #[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1769 | // `patternProperties.^bar` - (should be >=5), but valid for `patternProperties.spam$` |
| 1770 | #[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1771 | // `patternProperties.spam$` - (should be <=10), but valid for `patternProperties.^bar` |
| 1772 | #[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1773 | // All combined + valid via `additionalProperties` |
| 1774 | #[test_case( |
| 1775 | &json!({"bar": 4, "spam": 11, "foo": 3, "faz": "a", "fam": 42}), |
| 1776 | &[ |
| 1777 | "4 is less than the minimum of 5", |
| 1778 | "\"a\" is not of type \"integer\"", |
| 1779 | "3 is not of type \"string\"", |
| 1780 | "11 is greater than the maximum of 10", |
| 1781 | ], |
| 1782 | &[ |
| 1783 | "/patternProperties/^bar/minimum", |
| 1784 | "/additionalProperties/type", |
| 1785 | "/properties/foo/type", |
| 1786 | "/patternProperties/spam$/maximum", |
| 1787 | ] |
| 1788 | )] |
| 1789 | fn schema_5_invalid(instance: &Value, expected: &[&str], locations: &[&str]) { |
| 1790 | let schema = schema_5(); |
| 1791 | tests_util::is_not_valid(&schema, instance); |
| 1792 | tests_util::expect_errors(&schema, instance, expected); |
| 1793 | tests_util::assert_locations(&schema, instance, locations); |
| 1794 | } |
| 1795 | |
| 1796 | fn schema_6() -> Value { |
| 1797 | // For `AdditionalPropertiesWithPatternsValidator` |
| 1798 | json!({ |
| 1799 | "additionalProperties": {"type": "integer"}, |
| 1800 | "patternProperties": { |
| 1801 | "^bar": {"type": "integer", "minimum": 5}, |
| 1802 | "spam$": {"type": "integer", "maximum": 10}, |
| 1803 | } |
| 1804 | }) |
| 1805 | } |
| 1806 | |
| 1807 | // Another type |
| 1808 | #[test_case(&json!([1]))] |
| 1809 | // The right type |
| 1810 | #[test_case(&json!({}))] |
| 1811 | // Match `additionalProperties` |
| 1812 | #[test_case(&json!({"faz": 42}))] |
| 1813 | // Match `patternProperties.^bar` |
| 1814 | #[test_case(&json!({"bar": 6}))] |
| 1815 | // Match `patternProperties.spam$` |
| 1816 | #[test_case(&json!({"spam": 7}))] |
| 1817 | // All `patternProperties` rules match on different values |
| 1818 | #[test_case(&json!({"bar": 6, "spam": 7}))] |
| 1819 | // All `patternProperties` rules match on the same value |
| 1820 | #[test_case(&json!({"barspam": 7}))] |
| 1821 | // All combined |
| 1822 | #[test_case(&json!({"barspam": 7, "bar": 6, "spam": 7, "faz": 42}))] |
| 1823 | fn schema_6_valid(instance: &Value) { |
| 1824 | let schema = schema_6(); |
| 1825 | tests_util::is_valid(&schema, instance); |
| 1826 | } |
| 1827 | |
| 1828 | // `additionalProperties` - extra keyword that doesn't match `additionalProperties` |
| 1829 | #[test_case(&json!({"faz": "a"}), &["\"a\" is not of type \"integer\""], &["/additionalProperties/type"])] |
| 1830 | #[test_case(&json!({"faz": "a", "haz": "a"}), &["\"a\" is not of type \"integer\"", "\"a\" is not of type \"integer\""], &["/additionalProperties/type", "/additionalProperties/type"])] |
| 1831 | // `additionalProperties` - should be an integer & `patternProperties.^bar` - invalid |
| 1832 | #[test_case(&json!({"foo": "a", "bar": 4}), &["4 is less than the minimum of 5", "\"a\" is not of type \"integer\""], &["/patternProperties/^bar/minimum", "/additionalProperties/type"])] |
| 1833 | // `patternProperties.^bar` (should be >=5) |
| 1834 | #[test_case(&json!({"bar": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1835 | // `patternProperties.spam$` (should be <=10) |
| 1836 | #[test_case(&json!({"spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1837 | // `patternProperties` - both values are invalid |
| 1838 | #[test_case(&json!({"bar": 4, "spam": 11}), &["4 is less than the minimum of 5", "11 is greater than the maximum of 10"], &["/patternProperties/^bar/minimum", "/patternProperties/spam$/maximum"])] |
| 1839 | // `patternProperties` - `bar` is valid, `spam` is invalid |
| 1840 | #[test_case(&json!({"bar": 6, "spam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1841 | // `patternProperties` - `bar` is invalid, `spam` is valid |
| 1842 | #[test_case(&json!({"bar": 4, "spam": 8}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1843 | // `patternProperties.^bar` - (should be >=5), but valid for `patternProperties.spam$` |
| 1844 | #[test_case(&json!({"barspam": 4}), &["4 is less than the minimum of 5"], &["/patternProperties/^bar/minimum"])] |
| 1845 | // `patternProperties.spam$` - (should be <=10), but valid for `patternProperties.^bar` |
| 1846 | #[test_case(&json!({"barspam": 11}), &["11 is greater than the maximum of 10"], &["/patternProperties/spam$/maximum"])] |
| 1847 | // All combined + valid via `additionalProperties` |
| 1848 | #[test_case( |
| 1849 | &json!({"bar": 4, "spam": 11, "faz": "a", "fam": 42}), |
| 1850 | &[ |
| 1851 | "4 is less than the minimum of 5", |
| 1852 | "\"a\" is not of type \"integer\"", |
| 1853 | "11 is greater than the maximum of 10", |
| 1854 | ], |
| 1855 | &[ |
| 1856 | "/patternProperties/^bar/minimum", |
| 1857 | "/additionalProperties/type", |
| 1858 | "/patternProperties/spam$/maximum", |
| 1859 | ] |
| 1860 | )] |
| 1861 | fn schema_6_invalid(instance: &Value, expected: &[&str], locations: &[&str]) { |
| 1862 | let schema = schema_6(); |
| 1863 | tests_util::is_not_valid(&schema, instance); |
| 1864 | tests_util::expect_errors(&schema, instance, expected); |
| 1865 | tests_util::assert_locations(&schema, instance, locations); |
| 1866 | } |
| 1867 | } |