| 1 | use crate::{ |
| 2 | compiler, |
| 3 | error::{error, no_error, ErrorIterator, ValidationError}, |
| 4 | node::SchemaNode, |
| 5 | paths::{LazyLocation, Location}, |
| 6 | types::JsonType, |
| 7 | validator::{EvaluationResult, Validate, ValidationContext}, |
| 8 | }; |
| 9 | use serde_json::{Map, Value}; |
| 10 | |
| 11 | use super::CompilationResult; |
| 12 | |
| 13 | pub(crate) struct AnyOfValidator { |
| 14 | schemas: Vec<SchemaNode>, |
| 15 | location: Location, |
| 16 | } |
| 17 | |
| 18 | impl AnyOfValidator { |
| 19 | #[inline] |
| 20 | pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 21 | if let Value::Array(items) = schema { |
| 22 | let ctx = ctx.new_at_location("anyOf"); |
| 23 | let mut schemas = Vec::with_capacity(items.len()); |
| 24 | for (idx, item) in items.iter().enumerate() { |
| 25 | let ctx = ctx.new_at_location(idx); |
| 26 | let node = compiler::compile(&ctx, ctx.as_resource_ref(item))?; |
| 27 | schemas.push(node); |
| 28 | } |
| 29 | Ok(Box::new(AnyOfValidator { |
| 30 | schemas, |
| 31 | location: ctx.location().clone(), |
| 32 | })) |
| 33 | } else { |
| 34 | Err(ValidationError::single_type_error( |
| 35 | Location::new(), |
| 36 | ctx.location().clone(), |
| 37 | schema, |
| 38 | JsonType::Array, |
| 39 | )) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl Validate for AnyOfValidator { |
| 45 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 46 | self.schemas.iter().any(|s| s.is_valid(instance, ctx)) |
| 47 | } |
| 48 | |
| 49 | fn validate<'i>( |
| 50 | &self, |
| 51 | instance: &'i Value, |
| 52 | location: &LazyLocation, |
| 53 | ctx: &mut ValidationContext, |
| 54 | ) -> Result<(), ValidationError<'i>> { |
| 55 | if self.is_valid(instance, ctx) { |
| 56 | Ok(()) |
| 57 | } else { |
| 58 | Err(ValidationError::any_of( |
| 59 | self.location.clone(), |
| 60 | location.into(), |
| 61 | instance, |
| 62 | self.schemas |
| 63 | .iter() |
| 64 | .map(|schema| schema.iter_errors(instance, location, ctx).collect()) |
| 65 | .collect(), |
| 66 | )) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn iter_errors<'i>( |
| 71 | &self, |
| 72 | instance: &'i Value, |
| 73 | location: &LazyLocation, |
| 74 | ctx: &mut ValidationContext, |
| 75 | ) -> ErrorIterator<'i> { |
| 76 | if self.is_valid(instance, ctx) { |
| 77 | no_error() |
| 78 | } else { |
| 79 | error(ValidationError::any_of( |
| 80 | self.location.clone(), |
| 81 | location.into(), |
| 82 | instance, |
| 83 | self.schemas |
| 84 | .iter() |
| 85 | .map(|schema| schema.iter_errors(instance, location, ctx).collect()) |
| 86 | .collect(), |
| 87 | )) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn evaluate( |
| 92 | &self, |
| 93 | instance: &Value, |
| 94 | location: &LazyLocation, |
| 95 | ctx: &mut ValidationContext, |
| 96 | ) -> EvaluationResult { |
| 97 | let total = self.schemas.len(); |
| 98 | let mut failures = Vec::with_capacity(total); |
| 99 | let mut iter = self.schemas.iter(); |
| 100 | while let Some(node) = iter.next() { |
| 101 | let result = node.evaluate_instance(instance, location, ctx); |
| 102 | if result.valid { |
| 103 | let remaining = total.saturating_sub(failures.len() + 1); |
| 104 | let mut successes = Vec::with_capacity(remaining + 1); |
| 105 | successes.push(result); |
| 106 | for node in iter { |
| 107 | let tail = node.evaluate_instance(instance, location, ctx); |
| 108 | if tail.valid { |
| 109 | successes.push(tail); |
| 110 | } |
| 111 | } |
| 112 | return EvaluationResult::from_children(successes); |
| 113 | } |
| 114 | failures.push(result); |
| 115 | } |
| 116 | EvaluationResult::from_children(failures) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[inline] |
| 121 | pub(crate) fn compile<'a>( |
| 122 | ctx: &compiler::Context, |
| 123 | _: &'a Map<String, Value>, |
| 124 | schema: &'a Value, |
| 125 | ) -> Option<CompilationResult<'a>> { |
| 126 | Some(AnyOfValidator::compile(ctx, schema)) |
| 127 | } |
| 128 | |
| 129 | #[cfg(test)] |
| 130 | mod tests { |
| 131 | use crate::tests_util; |
| 132 | use serde_json::{json, Value}; |
| 133 | use test_case::test_case; |
| 134 | |
| 135 | #[test_case(&json!({"anyOf": [{"type": "string"}]}), &json!(1), "/anyOf")] |
| 136 | #[test_case(&json!({"anyOf": [{"type": "integer"}, {"type": "string"}]}), &json!({}), "/anyOf")] |
| 137 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 138 | tests_util::assert_schema_location(schema, instance, expected); |
| 139 | } |
| 140 | } |