| 1 | use crate::{ |
| 2 | compiler, |
| 3 | 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 AllOfValidator { |
| 14 | schemas: Vec<SchemaNode>, |
| 15 | } |
| 16 | |
| 17 | impl AllOfValidator { |
| 18 | #[inline] |
| 19 | pub(crate) fn compile<'a>( |
| 20 | ctx: &compiler::Context, |
| 21 | items: &'a [Value], |
| 22 | ) -> CompilationResult<'a> { |
| 23 | let ctx = ctx.new_at_location("allOf"); |
| 24 | let mut schemas = Vec::with_capacity(items.len()); |
| 25 | for (idx, item) in items.iter().enumerate() { |
| 26 | let ctx = ctx.new_at_location(idx); |
| 27 | let validators = compiler::compile(&ctx, ctx.as_resource_ref(item))?; |
| 28 | schemas.push(validators); |
| 29 | } |
| 30 | Ok(Box::new(AllOfValidator { schemas })) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl Validate for AllOfValidator { |
| 35 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 36 | self.schemas.iter().all(|n| n.is_valid(instance, ctx)) |
| 37 | } |
| 38 | |
| 39 | fn validate<'i>( |
| 40 | &self, |
| 41 | instance: &'i Value, |
| 42 | location: &LazyLocation, |
| 43 | ctx: &mut ValidationContext, |
| 44 | ) -> Result<(), ValidationError<'i>> { |
| 45 | for schema in &self.schemas { |
| 46 | schema.validate(instance, location, ctx)?; |
| 47 | } |
| 48 | Ok(()) |
| 49 | } |
| 50 | |
| 51 | #[allow(clippy::needless_collect)] |
| 52 | fn iter_errors<'i>( |
| 53 | &self, |
| 54 | instance: &'i Value, |
| 55 | location: &LazyLocation, |
| 56 | ctx: &mut ValidationContext, |
| 57 | ) -> ErrorIterator<'i> { |
| 58 | let errors: Vec<_> = self |
| 59 | .schemas |
| 60 | .iter() |
| 61 | .flat_map(move |node| node.iter_errors(instance, location, ctx)) |
| 62 | .collect(); |
| 63 | ErrorIterator::from_iterator(errors.into_iter()) |
| 64 | } |
| 65 | |
| 66 | fn evaluate( |
| 67 | &self, |
| 68 | instance: &Value, |
| 69 | location: &LazyLocation, |
| 70 | ctx: &mut ValidationContext, |
| 71 | ) -> EvaluationResult { |
| 72 | let children = self |
| 73 | .schemas |
| 74 | .iter() |
| 75 | .map(move |node| node.evaluate_instance(instance, location, ctx)) |
| 76 | .collect(); |
| 77 | EvaluationResult::from_children(children) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub(crate) struct SingleValueAllOfValidator { |
| 82 | node: SchemaNode, |
| 83 | } |
| 84 | |
| 85 | impl SingleValueAllOfValidator { |
| 86 | #[inline] |
| 87 | pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 88 | let ctx = ctx.new_at_location("allOf"); |
| 89 | let ctx = ctx.new_at_location(0); |
| 90 | let node = compiler::compile(&ctx, ctx.as_resource_ref(schema))?; |
| 91 | Ok(Box::new(SingleValueAllOfValidator { node })) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl Validate for SingleValueAllOfValidator { |
| 96 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 97 | self.node.is_valid(instance, ctx) |
| 98 | } |
| 99 | |
| 100 | fn validate<'i>( |
| 101 | &self, |
| 102 | instance: &'i Value, |
| 103 | location: &LazyLocation, |
| 104 | ctx: &mut ValidationContext, |
| 105 | ) -> Result<(), ValidationError<'i>> { |
| 106 | self.node.validate(instance, location, ctx) |
| 107 | } |
| 108 | |
| 109 | fn iter_errors<'i>( |
| 110 | &self, |
| 111 | instance: &'i Value, |
| 112 | location: &LazyLocation, |
| 113 | ctx: &mut ValidationContext, |
| 114 | ) -> ErrorIterator<'i> { |
| 115 | self.node.iter_errors(instance, location, ctx) |
| 116 | } |
| 117 | |
| 118 | fn evaluate( |
| 119 | &self, |
| 120 | instance: &Value, |
| 121 | location: &LazyLocation, |
| 122 | ctx: &mut ValidationContext, |
| 123 | ) -> EvaluationResult { |
| 124 | EvaluationResult::from(self.node.evaluate_instance(instance, location, ctx)) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[inline] |
| 129 | pub(crate) fn compile<'a>( |
| 130 | ctx: &compiler::Context, |
| 131 | _: &'a Map<String, Value>, |
| 132 | schema: &'a Value, |
| 133 | ) -> Option<CompilationResult<'a>> { |
| 134 | if let Value::Array(items) = schema { |
| 135 | if items.len() == 1 { |
| 136 | let value = items.iter().next().expect("Vec is not empty"); |
| 137 | Some(SingleValueAllOfValidator::compile(ctx, value)) |
| 138 | } else { |
| 139 | Some(AllOfValidator::compile(ctx, items)) |
| 140 | } |
| 141 | } else { |
| 142 | Some(Err(ValidationError::single_type_error( |
| 143 | Location::new(), |
| 144 | ctx.location().clone(), |
| 145 | schema, |
| 146 | JsonType::Array, |
| 147 | ))) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #[cfg(test)] |
| 152 | mod tests { |
| 153 | use crate::tests_util; |
| 154 | use serde_json::{json, Value}; |
| 155 | use test_case::test_case; |
| 156 | |
| 157 | #[test_case(&json!({"allOf": [{"type": "string"}]}), &json!(1), "/allOf/0/type")] |
| 158 | #[test_case(&json!({"allOf": [{"type": "integer"}, {"maximum": 5}]}), &json!(6), "/allOf/1/maximum")] |
| 159 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 160 | tests_util::assert_schema_location(schema, instance, expected); |
| 161 | } |
| 162 | } |