| 1 | use crate::{ |
| 2 | compiler, |
| 3 | error::{no_error, ErrorIterator, ValidationError}, |
| 4 | keywords::{required, unique_items, CompilationResult}, |
| 5 | node::SchemaNode, |
| 6 | paths::{LazyLocation, Location}, |
| 7 | types::JsonType, |
| 8 | validator::{EvaluationResult, Validate, ValidationContext}, |
| 9 | }; |
| 10 | use serde_json::{Map, Value}; |
| 11 | |
| 12 | pub(crate) struct DependenciesValidator { |
| 13 | dependencies: Vec<(String, SchemaNode)>, |
| 14 | } |
| 15 | |
| 16 | impl DependenciesValidator { |
| 17 | #[inline] |
| 18 | pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 19 | if let Value::Object(map) = schema { |
| 20 | let kctx = ctx.new_at_location("dependencies"); |
| 21 | let mut dependencies = Vec::with_capacity(map.len()); |
| 22 | for (key, subschema) in map { |
| 23 | let ctx = kctx.new_at_location(key.as_str()); |
| 24 | let s = |
| 25 | match subschema { |
| 26 | Value::Array(_) => { |
| 27 | let validators = vec![required::compile_with_path( |
| 28 | subschema, |
| 29 | kctx.location().clone(), |
| 30 | ) |
| 31 | .expect("The required validator compilation does not return None")?]; |
| 32 | SchemaNode::from_array(&kctx, validators) |
| 33 | } |
| 34 | _ => compiler::compile(&ctx, ctx.as_resource_ref(subschema))?, |
| 35 | }; |
| 36 | dependencies.push((key.clone(), s)); |
| 37 | } |
| 38 | Ok(Box::new(DependenciesValidator { dependencies })) |
| 39 | } else { |
| 40 | Err(ValidationError::single_type_error( |
| 41 | Location::new(), |
| 42 | ctx.location().clone(), |
| 43 | schema, |
| 44 | JsonType::Object, |
| 45 | )) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl Validate for DependenciesValidator { |
| 51 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 52 | if let Value::Object(item) = instance { |
| 53 | for (property, node) in &self.dependencies { |
| 54 | if item.contains_key(property) && !node.is_valid(instance, ctx) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | true |
| 59 | } else { |
| 60 | true |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn validate<'i>( |
| 65 | &self, |
| 66 | instance: &'i Value, |
| 67 | location: &LazyLocation, |
| 68 | ctx: &mut ValidationContext, |
| 69 | ) -> Result<(), ValidationError<'i>> { |
| 70 | if let Value::Object(item) = instance { |
| 71 | for (property, dependency) in &self.dependencies { |
| 72 | if item.contains_key(property) { |
| 73 | dependency.validate(instance, location, ctx)?; |
| 74 | } |
| 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 (property, node) in &self.dependencies { |
| 89 | if item.contains_key(property) { |
| 90 | errors.extend(node.iter_errors(instance, location, ctx)); |
| 91 | } |
| 92 | } |
| 93 | ErrorIterator::from_iterator(errors.into_iter()) |
| 94 | } else { |
| 95 | no_error() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | fn evaluate( |
| 100 | &self, |
| 101 | instance: &Value, |
| 102 | location: &LazyLocation, |
| 103 | ctx: &mut ValidationContext, |
| 104 | ) -> EvaluationResult { |
| 105 | if let Value::Object(item) = instance { |
| 106 | let mut children = Vec::new(); |
| 107 | for (property, dependency) in &self.dependencies { |
| 108 | if item.contains_key(property) { |
| 109 | children.push(dependency.evaluate_instance(instance, location, ctx)); |
| 110 | } |
| 111 | } |
| 112 | EvaluationResult::from_children(children) |
| 113 | } else { |
| 114 | EvaluationResult::valid_empty() |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub(crate) struct DependentRequiredValidator { |
| 120 | dependencies: Vec<(String, SchemaNode)>, |
| 121 | } |
| 122 | |
| 123 | impl DependentRequiredValidator { |
| 124 | #[inline] |
| 125 | pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 126 | if let Value::Object(map) = schema { |
| 127 | let kctx = ctx.new_at_location("dependentRequired"); |
| 128 | let mut dependencies = Vec::with_capacity(map.len()); |
| 129 | for (key, subschema) in map { |
| 130 | let ictx = kctx.new_at_location(key.as_str()); |
| 131 | if let Value::Array(dependency_array) = subschema { |
| 132 | if !unique_items::is_unique(dependency_array) { |
| 133 | return Err(ValidationError::unique_items( |
| 134 | Location::new(), |
| 135 | ictx.location().clone(), |
| 136 | subschema, |
| 137 | )); |
| 138 | } |
| 139 | let validators = |
| 140 | vec![ |
| 141 | required::compile_with_path(subschema, kctx.location().clone()) |
| 142 | .expect( |
| 143 | "The required validator compilation does not return None", |
| 144 | )?, |
| 145 | ]; |
| 146 | dependencies.push((key.clone(), SchemaNode::from_array(&kctx, validators))); |
| 147 | } else { |
| 148 | return Err(ValidationError::single_type_error( |
| 149 | Location::new(), |
| 150 | ictx.location().clone(), |
| 151 | subschema, |
| 152 | JsonType::Array, |
| 153 | )); |
| 154 | } |
| 155 | } |
| 156 | Ok(Box::new(DependentRequiredValidator { dependencies })) |
| 157 | } else { |
| 158 | Err(ValidationError::single_type_error( |
| 159 | Location::new(), |
| 160 | ctx.location().clone(), |
| 161 | schema, |
| 162 | JsonType::Object, |
| 163 | )) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | impl Validate for DependentRequiredValidator { |
| 168 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 169 | if let Value::Object(item) = instance { |
| 170 | for (property, node) in &self.dependencies { |
| 171 | if item.contains_key(property) && !node.is_valid(instance, ctx) { |
| 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | true |
| 176 | } else { |
| 177 | true |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn validate<'i>( |
| 182 | &self, |
| 183 | instance: &'i Value, |
| 184 | location: &LazyLocation, |
| 185 | ctx: &mut ValidationContext, |
| 186 | ) -> Result<(), ValidationError<'i>> { |
| 187 | if let Value::Object(item) = instance { |
| 188 | for (property, dependency) in &self.dependencies { |
| 189 | if item.contains_key(property) { |
| 190 | dependency.validate(instance, location, ctx)?; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | Ok(()) |
| 195 | } |
| 196 | |
| 197 | fn iter_errors<'i>( |
| 198 | &self, |
| 199 | instance: &'i Value, |
| 200 | location: &LazyLocation, |
| 201 | ctx: &mut ValidationContext, |
| 202 | ) -> ErrorIterator<'i> { |
| 203 | if let Value::Object(item) = instance { |
| 204 | let mut errors = Vec::new(); |
| 205 | for (property, node) in &self.dependencies { |
| 206 | if item.contains_key(property) { |
| 207 | errors.extend(node.iter_errors(instance, location, ctx)); |
| 208 | } |
| 209 | } |
| 210 | ErrorIterator::from_iterator(errors.into_iter()) |
| 211 | } else { |
| 212 | no_error() |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | fn evaluate( |
| 217 | &self, |
| 218 | instance: &Value, |
| 219 | location: &LazyLocation, |
| 220 | ctx: &mut ValidationContext, |
| 221 | ) -> EvaluationResult { |
| 222 | if let Value::Object(item) = instance { |
| 223 | let mut children = Vec::new(); |
| 224 | for (property, dependency) in &self.dependencies { |
| 225 | if item.contains_key(property) { |
| 226 | children.push(dependency.evaluate_instance(instance, location, ctx)); |
| 227 | } |
| 228 | } |
| 229 | EvaluationResult::from_children(children) |
| 230 | } else { |
| 231 | EvaluationResult::valid_empty() |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | pub(crate) struct DependentSchemasValidator { |
| 237 | dependencies: Vec<(String, SchemaNode)>, |
| 238 | } |
| 239 | impl DependentSchemasValidator { |
| 240 | #[inline] |
| 241 | pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> { |
| 242 | if let Value::Object(map) = schema { |
| 243 | let ctx = ctx.new_at_location("dependentSchemas"); |
| 244 | let mut dependencies = Vec::with_capacity(map.len()); |
| 245 | for (key, subschema) in map { |
| 246 | let ctx = ctx.new_at_location(key.as_str()); |
| 247 | let schema_nodes = compiler::compile(&ctx, ctx.as_resource_ref(subschema))?; |
| 248 | dependencies.push((key.clone(), schema_nodes)); |
| 249 | } |
| 250 | Ok(Box::new(DependentSchemasValidator { dependencies })) |
| 251 | } else { |
| 252 | Err(ValidationError::single_type_error( |
| 253 | Location::new(), |
| 254 | ctx.location().clone(), |
| 255 | schema, |
| 256 | JsonType::Object, |
| 257 | )) |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | impl Validate for DependentSchemasValidator { |
| 262 | fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool { |
| 263 | if let Value::Object(item) = instance { |
| 264 | for (property, node) in &self.dependencies { |
| 265 | if item.contains_key(property) && !node.is_valid(instance, ctx) { |
| 266 | return false; |
| 267 | } |
| 268 | } |
| 269 | true |
| 270 | } else { |
| 271 | true |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | fn validate<'i>( |
| 276 | &self, |
| 277 | instance: &'i Value, |
| 278 | location: &LazyLocation, |
| 279 | ctx: &mut ValidationContext, |
| 280 | ) -> Result<(), ValidationError<'i>> { |
| 281 | if let Value::Object(item) = instance { |
| 282 | for (property, dependency) in &self.dependencies { |
| 283 | if item.contains_key(property) { |
| 284 | dependency.validate(instance, location, ctx)?; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | Ok(()) |
| 289 | } |
| 290 | |
| 291 | fn iter_errors<'i>( |
| 292 | &self, |
| 293 | instance: &'i Value, |
| 294 | location: &LazyLocation, |
| 295 | ctx: &mut ValidationContext, |
| 296 | ) -> ErrorIterator<'i> { |
| 297 | if let Value::Object(item) = instance { |
| 298 | let mut errors = Vec::new(); |
| 299 | for (property, node) in &self.dependencies { |
| 300 | if item.contains_key(property) { |
| 301 | errors.extend(node.iter_errors(instance, location, ctx)); |
| 302 | } |
| 303 | } |
| 304 | ErrorIterator::from_iterator(errors.into_iter()) |
| 305 | } else { |
| 306 | no_error() |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | fn evaluate( |
| 311 | &self, |
| 312 | instance: &Value, |
| 313 | location: &LazyLocation, |
| 314 | ctx: &mut ValidationContext, |
| 315 | ) -> EvaluationResult { |
| 316 | if let Value::Object(item) = instance { |
| 317 | let mut children = Vec::new(); |
| 318 | for (property, dependency) in &self.dependencies { |
| 319 | if item.contains_key(property) { |
| 320 | children.push(dependency.evaluate_instance(instance, location, ctx)); |
| 321 | } |
| 322 | } |
| 323 | EvaluationResult::from_children(children) |
| 324 | } else { |
| 325 | EvaluationResult::valid_empty() |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #[inline] |
| 331 | pub(crate) fn compile<'a>( |
| 332 | ctx: &compiler::Context, |
| 333 | _: &'a Map<String, Value>, |
| 334 | schema: &'a Value, |
| 335 | ) -> Option<CompilationResult<'a>> { |
| 336 | Some(DependenciesValidator::compile(ctx, schema)) |
| 337 | } |
| 338 | #[inline] |
| 339 | pub(crate) fn compile_dependent_required<'a>( |
| 340 | ctx: &compiler::Context, |
| 341 | _: &'a Map<String, Value>, |
| 342 | schema: &'a Value, |
| 343 | ) -> Option<CompilationResult<'a>> { |
| 344 | Some(DependentRequiredValidator::compile(ctx, schema)) |
| 345 | } |
| 346 | #[inline] |
| 347 | pub(crate) fn compile_dependent_schemas<'a>( |
| 348 | ctx: &compiler::Context, |
| 349 | _: &'a Map<String, Value>, |
| 350 | schema: &'a Value, |
| 351 | ) -> Option<CompilationResult<'a>> { |
| 352 | Some(DependentSchemasValidator::compile(ctx, schema)) |
| 353 | } |
| 354 | #[cfg(test)] |
| 355 | mod tests { |
| 356 | use crate::tests_util; |
| 357 | use serde_json::{json, Value}; |
| 358 | use test_case::test_case; |
| 359 | |
| 360 | #[test_case(&json!({"dependencies": {"bar": ["foo"]}}), &json!({"bar": 1}), "/dependencies")] |
| 361 | #[test_case(&json!({"dependencies": {"bar": {"type": "string"}}}), &json!({"bar": 1}), "/dependencies/bar/type")] |
| 362 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 363 | tests_util::assert_schema_location(schema, instance, expected); |
| 364 | } |
| 365 | } |