| 1 | use serde_json::Value; |
| 2 | |
| 3 | use crate::{resource::InnerResourcePtr, Error, Resolver, Segments}; |
| 4 | |
| 5 | use super::subresources::{self, SubresourceIteratorInner}; |
| 6 | |
| 7 | pub(crate) fn object_iter<'a>( |
| 8 | (key, value): (&'a String, &'a Value), |
| 9 | ) -> SubresourceIteratorInner<'a> { |
| 10 | match key.as_str() { |
| 11 | // For "items": if it’s an array, iterate over it; otherwise, yield one element. |
| 12 | "items" => match value { |
| 13 | Value::Array(arr) => SubresourceIteratorInner::Array(arr.iter()), |
| 14 | _ => SubresourceIteratorInner::Once(value), |
| 15 | }, |
| 16 | // For "allOf", "anyOf", "oneOf", "prefixItems": if the value is an array, iterate over it. |
| 17 | "allOf" | "anyOf" | "oneOf" | "prefixItems" => { |
| 18 | if let Some(arr) = value.as_array() { |
| 19 | SubresourceIteratorInner::Array(arr.iter()) |
| 20 | } else { |
| 21 | SubresourceIteratorInner::Empty |
| 22 | } |
| 23 | } |
| 24 | // For "$defs", "definitions", "dependentSchemas", "patternProperties", "properties": |
| 25 | // if the value is an object, iterate over its values. |
| 26 | "$defs" | "definitions" | "dependentSchemas" | "patternProperties" | "properties" => { |
| 27 | if let Some(obj) = value.as_object() { |
| 28 | SubresourceIteratorInner::Object(obj.values()) |
| 29 | } else { |
| 30 | SubresourceIteratorInner::Empty |
| 31 | } |
| 32 | } |
| 33 | // For "dependencies": if the value is an object, iterate over its values filtered to only those that are objects. |
| 34 | "dependencies" => { |
| 35 | if let Some(obj) = value.as_object() { |
| 36 | SubresourceIteratorInner::FilteredObject(obj.values()) |
| 37 | } else { |
| 38 | SubresourceIteratorInner::Empty |
| 39 | } |
| 40 | } |
| 41 | // For "additionalItems" and "additionalProperties", only if the value is an object. |
| 42 | "additionalItems" | "additionalProperties" if value.is_object() => { |
| 43 | SubresourceIteratorInner::Once(value) |
| 44 | } |
| 45 | // For other keys that were originally in the “single element” group: |
| 46 | "contains" |
| 47 | | "contentSchema" |
| 48 | | "else" |
| 49 | | "if" |
| 50 | | "propertyNames" |
| 51 | | "not" |
| 52 | | "then" |
| 53 | | "unevaluatedItems" |
| 54 | | "unevaluatedProperties" => SubresourceIteratorInner::Once(value), |
| 55 | _ => SubresourceIteratorInner::Empty, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub(crate) fn maybe_in_subresource<'r>( |
| 60 | segments: &Segments, |
| 61 | resolver: &Resolver<'r>, |
| 62 | subresource: &InnerResourcePtr, |
| 63 | ) -> Result<Resolver<'r>, Error> { |
| 64 | const IN_VALUE: &[&str] = &["additionalItems", "additionalProperties", "not"]; |
| 65 | const IN_CHILD: &[&str] = &[ |
| 66 | "allOf", |
| 67 | "anyOf", |
| 68 | "oneOf", |
| 69 | "definitions", |
| 70 | "patternProperties", |
| 71 | "properties", |
| 72 | ]; |
| 73 | subresources::maybe_in_subresource_with_items_and_dependencies( |
| 74 | segments, |
| 75 | resolver, |
| 76 | subresource, |
| 77 | IN_VALUE, |
| 78 | IN_CHILD, |
| 79 | ) |
| 80 | } |