| 1 | use crate::{ |
| 2 | compiler, |
| 3 | error::{no_error, ErrorIterator}, |
| 4 | evaluation::Annotations, |
| 5 | keywords::CompilationResult, |
| 6 | node::SchemaNode, |
| 7 | paths::LazyLocation, |
| 8 | validator::{EvaluationResult, Validate, ValidationContext}, |
| 9 | ValidationError, |
| 10 | }; |
| 11 | use serde_json::{Map, Value}; |
| 12 | |
| 13 | pub(crate) struct ItemsArrayValidator { |
| 14 | items: Vec<SchemaNode>, |
| 15 | } |
| 16 | impl ItemsArrayValidator { |
| 17 | #[inline] |
| 18 | pub(crate) fn compile<'a>( |
| 19 | ctx: &compiler::Context, |
| 20 | schemas: &'a [Value], |
| 21 | ) -> CompilationResult<'a> { |
| 22 | let kctx = ctx.new_at_location("items"); |
| 23 | let mut items = Vec::with_capacity(schemas.len()); |
| 24 | for (idx, item) in schemas.iter().enumerate() { |
| 25 | let ictx = kctx.new_at_location(idx); |
| 26 | let validators = compiler::compile(&ictx, ictx.as_resource_ref(item))?; |
| 27 | items.push(validators); |
| 28 | } |
| 29 | Ok(Box::new(ItemsArrayValidator { items })) |
| 30 | } |
| 31 | } |
| 32 | impl Validate for ItemsArrayValidator { |
| 33 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 34 | if let Value::Array(items) = instance { |
| 35 | for (item, node) in items.iter().zip(self.items.iter()) { |
| 36 | if !node.is_valid(item, ctx) { |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | true |
| 41 | } else { |
| 42 | true |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn validate<'i>( |
| 47 | &self, |
| 48 | instance: &'i Value, |
| 49 | location: &LazyLocation, |
| 50 | ctx: &mut ValidationContext, |
| 51 | ) -> Result<(), ValidationError<'i>> { |
| 52 | if let Value::Array(items) = instance { |
| 53 | for (idx, (item, node)) in items.iter().zip(self.items.iter()).enumerate() { |
| 54 | node.validate(item, &location.push(idx), ctx)?; |
| 55 | } |
| 56 | } |
| 57 | Ok(()) |
| 58 | } |
| 59 | |
| 60 | fn iter_errors<'i>( |
| 61 | &self, |
| 62 | instance: &'i Value, |
| 63 | location: &LazyLocation, |
| 64 | ctx: &mut ValidationContext, |
| 65 | ) -> ErrorIterator<'i> { |
| 66 | if let Value::Array(items) = instance { |
| 67 | let mut errors = Vec::new(); |
| 68 | for (idx, (item, node)) in items.iter().zip(self.items.iter()).enumerate() { |
| 69 | errors.extend(node.iter_errors(item, &location.push(idx), ctx)); |
| 70 | } |
| 71 | ErrorIterator::from_iterator(errors.into_iter()) |
| 72 | } else { |
| 73 | no_error() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn evaluate( |
| 78 | &self, |
| 79 | instance: &Value, |
| 80 | location: &LazyLocation, |
| 81 | ctx: &mut ValidationContext, |
| 82 | ) -> EvaluationResult { |
| 83 | if let Value::Array(items) = instance { |
| 84 | let mut children = Vec::with_capacity(self.items.len().min(items.len())); |
| 85 | for (idx, (item, node)) in items.iter().zip(self.items.iter()).enumerate() { |
| 86 | children.push(node.evaluate_instance(item, &location.push(idx), ctx)); |
| 87 | } |
| 88 | EvaluationResult::from_children(children) |
| 89 | } else { |
| 90 | EvaluationResult::valid_empty() |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub(crate) struct ItemsObjectValidator { |
| 96 | node: SchemaNode, |
| 97 | } |
| 98 | |
| 99 | impl ItemsObjectValidator { |
| 100 | #[inline] |
| 101 | pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 102 | let ctx = ctx.new_at_location("items"); |
| 103 | let node = compiler::compile(&ctx, ctx.as_resource_ref(schema))?; |
| 104 | Ok(Box::new(ItemsObjectValidator { node })) |
| 105 | } |
| 106 | } |
| 107 | impl Validate for ItemsObjectValidator { |
| 108 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 109 | if let Value::Array(items) = instance { |
| 110 | items.iter().all(|i| self.node.is_valid(i, ctx)) |
| 111 | } else { |
| 112 | true |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn validate<'i>( |
| 117 | &self, |
| 118 | instance: &'i Value, |
| 119 | location: &LazyLocation, |
| 120 | ctx: &mut ValidationContext, |
| 121 | ) -> Result<(), ValidationError<'i>> { |
| 122 | if let Value::Array(items) = instance { |
| 123 | for (idx, item) in items.iter().enumerate() { |
| 124 | self.node.validate(item, &location.push(idx), ctx)?; |
| 125 | } |
| 126 | } |
| 127 | Ok(()) |
| 128 | } |
| 129 | |
| 130 | fn iter_errors<'i>( |
| 131 | &self, |
| 132 | instance: &'i Value, |
| 133 | location: &LazyLocation, |
| 134 | ctx: &mut ValidationContext, |
| 135 | ) -> ErrorIterator<'i> { |
| 136 | if let Value::Array(items) = instance { |
| 137 | let mut errors = Vec::new(); |
| 138 | for (idx, item) in items.iter().enumerate() { |
| 139 | errors.extend(self.node.iter_errors(item, &location.push(idx), ctx)); |
| 140 | } |
| 141 | ErrorIterator::from_iterator(errors.into_iter()) |
| 142 | } else { |
| 143 | no_error() |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn evaluate( |
| 148 | &self, |
| 149 | instance: &Value, |
| 150 | location: &LazyLocation, |
| 151 | ctx: &mut ValidationContext, |
| 152 | ) -> EvaluationResult { |
| 153 | if let Value::Array(items) = instance { |
| 154 | let mut children = Vec::with_capacity(items.len()); |
| 155 | for (idx, item) in items.iter().enumerate() { |
| 156 | children.push(self.node.evaluate_instance(item, &location.push(idx), ctx)); |
| 157 | } |
| 158 | let schema_was_applied = !items.is_empty(); |
| 159 | let mut result = EvaluationResult::from_children(children); |
| 160 | result.annotate(Annotations::new(serde_json::json!(schema_was_applied))); |
| 161 | result |
| 162 | } else { |
| 163 | EvaluationResult::valid_empty() |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub(crate) struct ItemsObjectSkipPrefixValidator { |
| 169 | node: SchemaNode, |
| 170 | skip_prefix: usize, |
| 171 | } |
| 172 | |
| 173 | impl ItemsObjectSkipPrefixValidator { |
| 174 | #[inline] |
| 175 | pub(crate) fn compile<'a>( |
| 176 | schema: &'a Value, |
| 177 | skip_prefix: usize, |
| 178 | ctx: &compiler::Context, |
| 179 | ) -> CompilationResult<'a> { |
| 180 | let ctx = ctx.new_at_location("items"); |
| 181 | let node = compiler::compile(&ctx, ctx.as_resource_ref(schema))?; |
| 182 | Ok(Box::new(ItemsObjectSkipPrefixValidator { |
| 183 | node, |
| 184 | skip_prefix, |
| 185 | })) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl Validate for ItemsObjectSkipPrefixValidator { |
| 190 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 191 | if let Value::Array(items) = instance { |
| 192 | items |
| 193 | .iter() |
| 194 | .skip(self.skip_prefix) |
| 195 | .all(|i| self.node.is_valid(i, ctx)) |
| 196 | } else { |
| 197 | true |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | fn validate<'i>( |
| 202 | &self, |
| 203 | instance: &'i Value, |
| 204 | location: &LazyLocation, |
| 205 | ctx: &mut ValidationContext, |
| 206 | ) -> Result<(), ValidationError<'i>> { |
| 207 | if let Value::Array(items) = instance { |
| 208 | for (idx, item) in items.iter().skip(self.skip_prefix).enumerate() { |
| 209 | self.node |
| 210 | .validate(item, &location.push(idx + self.skip_prefix), ctx)?; |
| 211 | } |
| 212 | } |
| 213 | Ok(()) |
| 214 | } |
| 215 | |
| 216 | fn iter_errors<'i>( |
| 217 | &self, |
| 218 | instance: &'i Value, |
| 219 | location: &LazyLocation, |
| 220 | ctx: &mut ValidationContext, |
| 221 | ) -> ErrorIterator<'i> { |
| 222 | if let Value::Array(items) = instance { |
| 223 | let mut errors = Vec::new(); |
| 224 | for (idx, item) in items.iter().skip(self.skip_prefix).enumerate() { |
| 225 | errors.extend(self.node.iter_errors( |
| 226 | item, |
| 227 | &location.push(idx + self.skip_prefix), |
| 228 | ctx, |
| 229 | )); |
| 230 | } |
| 231 | ErrorIterator::from_iterator(errors.into_iter()) |
| 232 | } else { |
| 233 | no_error() |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | fn evaluate( |
| 238 | &self, |
| 239 | instance: &Value, |
| 240 | location: &LazyLocation, |
| 241 | ctx: &mut ValidationContext, |
| 242 | ) -> EvaluationResult { |
| 243 | if let Value::Array(items) = instance { |
| 244 | let mut children = Vec::with_capacity(items.len().saturating_sub(self.skip_prefix)); |
| 245 | for (idx, item) in items.iter().enumerate().skip(self.skip_prefix) { |
| 246 | children.push(self.node.evaluate_instance(item, &location.push(idx), ctx)); |
| 247 | } |
| 248 | let schema_was_applied = items.len() > self.skip_prefix; |
| 249 | let mut result = EvaluationResult::from_children(children); |
| 250 | result.annotate(Annotations::new(serde_json::json!(schema_was_applied))); |
| 251 | result |
| 252 | } else { |
| 253 | EvaluationResult::valid_empty() |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | #[inline] |
| 259 | pub(crate) fn compile<'a>( |
| 260 | ctx: &compiler::Context, |
| 261 | parent: &'a Map<String, Value>, |
| 262 | schema: &'a Value, |
| 263 | ) -> Option<CompilationResult<'a>> { |
| 264 | match schema { |
| 265 | Value::Array(items) => Some(ItemsArrayValidator::compile(ctx, items)), |
| 266 | Value::Object(_) | Value::Bool(false) => { |
| 267 | if let Some(Value::Array(prefix_items)) = parent.get("prefixItems") { |
| 268 | return Some(ItemsObjectSkipPrefixValidator::compile( |
| 269 | schema, |
| 270 | prefix_items.len(), |
| 271 | ctx, |
| 272 | )); |
| 273 | } |
| 274 | Some(ItemsObjectValidator::compile(ctx, schema)) |
| 275 | } |
| 276 | _ => None, |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | #[cfg(test)] |
| 281 | mod tests { |
| 282 | use crate::tests_util; |
| 283 | use serde_json::{json, Value}; |
| 284 | use test_case::test_case; |
| 285 | |
| 286 | #[test_case(&json!({"items": false}), &json!([1]), "/items")] |
| 287 | #[test_case(&json!({"items": {"type": "string"}}), &json!([1]), "/items/type")] |
| 288 | #[test_case(&json!({"prefixItems": [{"type": "string"}]}), &json!([1]), "/prefixItems/0/type")] |
| 289 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 290 | tests_util::assert_schema_location(schema, instance, expected); |
| 291 | } |
| 292 | } |