| 1 | use serde_json::Value; |
| 2 | use subresources::SubresourceIterator; |
| 3 | |
| 4 | mod draft201909; |
| 5 | mod draft4; |
| 6 | mod draft6; |
| 7 | mod draft7; |
| 8 | mod ids; |
| 9 | mod subresources; |
| 10 | |
| 11 | use crate::{ |
| 12 | anchors, |
| 13 | resource::InnerResourcePtr, |
| 14 | vocabularies::{VocabularySet, DRAFT_2019_09_VOCABULARIES, DRAFT_2020_12_VOCABULARIES}, |
| 15 | Anchor, Error, Resolver, Resource, ResourceRef, Segments, |
| 16 | }; |
| 17 | |
| 18 | /// JSON Schema specification versions. |
| 19 | #[non_exhaustive] |
| 20 | #[derive(Debug, Default, PartialEq, Copy, Clone, Hash, Eq, PartialOrd, Ord)] |
| 21 | pub enum Draft { |
| 22 | /// JSON Schema Draft 4 |
| 23 | Draft4, |
| 24 | /// JSON Schema Draft 6 |
| 25 | Draft6, |
| 26 | /// JSON Schema Draft 7 |
| 27 | Draft7, |
| 28 | /// JSON Schema Draft 2019-09 |
| 29 | Draft201909, |
| 30 | /// JSON Schema Draft 2020-12 |
| 31 | #[default] |
| 32 | Draft202012, |
| 33 | /// Internal use only: Represents custom/unrecognized meta-schemas. |
| 34 | /// Do not use directly. Custom meta-schemas are resolved automatically |
| 35 | /// when registered in the Registry. |
| 36 | #[doc(hidden)] |
| 37 | Unknown, |
| 38 | } |
| 39 | |
| 40 | impl Draft { |
| 41 | #[must_use] |
| 42 | pub fn create_resource(self, contents: Value) -> Resource { |
| 43 | Resource::new(contents, self) |
| 44 | } |
| 45 | #[must_use] |
| 46 | pub fn create_resource_ref(self, contents: &Value) -> ResourceRef<'_> { |
| 47 | ResourceRef::new(contents, self) |
| 48 | } |
| 49 | /// Detect what specification could be applied to the given contents. |
| 50 | /// |
| 51 | /// Returns `Draft::Unknown` for custom/unknown `$schema` values. |
| 52 | /// Validation of custom meta-schemas happens during registry building. |
| 53 | #[must_use] |
| 54 | pub fn detect(self, contents: &Value) -> Draft { |
| 55 | if let Some(schema) = contents |
| 56 | .as_object() |
| 57 | .and_then(|contents| contents.get("$schema")) |
| 58 | .and_then(|schema| schema.as_str()) |
| 59 | { |
| 60 | match schema.trim_end_matches('#') { |
| 61 | // Accept both HTTPS and HTTP for all known drafts |
| 62 | "https://json-schema.org/draft/2020-12/schema" |
| 63 | | "http://json-schema.org/draft/2020-12/schema" => Draft::Draft202012, |
| 64 | "https://json-schema.org/draft/2019-09/schema" |
| 65 | | "http://json-schema.org/draft/2019-09/schema" => Draft::Draft201909, |
| 66 | "https://json-schema.org/draft-07/schema" |
| 67 | | "http://json-schema.org/draft-07/schema" => Draft::Draft7, |
| 68 | "https://json-schema.org/draft-06/schema" |
| 69 | | "http://json-schema.org/draft-06/schema" => Draft::Draft6, |
| 70 | "https://json-schema.org/draft-04/schema" |
| 71 | | "http://json-schema.org/draft-04/schema" => Draft::Draft4, |
| 72 | // Custom/unknown meta-schemas return Unknown |
| 73 | // Validation of custom meta-schemas happens during registry building |
| 74 | _ => Draft::Unknown, |
| 75 | } |
| 76 | } else { |
| 77 | self |
| 78 | } |
| 79 | } |
| 80 | pub(crate) fn id_of(self, contents: &Value) -> Option<&str> { |
| 81 | match self { |
| 82 | Draft::Draft4 => ids::legacy_id(contents), |
| 83 | Draft::Draft6 | Draft::Draft7 => ids::legacy_dollar_id(contents), |
| 84 | Draft::Draft201909 | Draft::Draft202012 | Draft::Unknown => ids::dollar_id(contents), |
| 85 | } |
| 86 | } |
| 87 | pub fn subresources_of(self, contents: &Value) -> impl Iterator<Item = &Value> { |
| 88 | match contents.as_object() { |
| 89 | Some(schema) => { |
| 90 | let object_iter = match self { |
| 91 | Draft::Draft4 => draft4::object_iter, |
| 92 | Draft::Draft6 => draft6::object_iter, |
| 93 | Draft::Draft7 => draft7::object_iter, |
| 94 | Draft::Draft201909 => draft201909::object_iter, |
| 95 | Draft::Draft202012 | Draft::Unknown => subresources::object_iter, |
| 96 | }; |
| 97 | SubresourceIterator::Object(schema.iter().flat_map(object_iter)) |
| 98 | } |
| 99 | None => SubresourceIterator::Empty, |
| 100 | } |
| 101 | } |
| 102 | pub(crate) fn anchors(self, contents: &Value) -> impl Iterator<Item = Anchor> { |
| 103 | match self { |
| 104 | Draft::Draft4 => anchors::legacy_anchor_in_id(self, contents), |
| 105 | Draft::Draft6 | Draft::Draft7 => anchors::legacy_anchor_in_dollar_id(self, contents), |
| 106 | Draft::Draft201909 => anchors::anchor_2019(self, contents), |
| 107 | Draft::Draft202012 | Draft::Unknown => anchors::anchor(self, contents), |
| 108 | } |
| 109 | } |
| 110 | pub(crate) fn maybe_in_subresource<'r>( |
| 111 | self, |
| 112 | segments: &Segments, |
| 113 | resolver: &Resolver<'r>, |
| 114 | subresource: &InnerResourcePtr, |
| 115 | ) -> Result<Resolver<'r>, Error> { |
| 116 | match self { |
| 117 | Draft::Draft4 => draft4::maybe_in_subresource(segments, resolver, subresource), |
| 118 | Draft::Draft6 => draft6::maybe_in_subresource(segments, resolver, subresource), |
| 119 | Draft::Draft7 => draft7::maybe_in_subresource(segments, resolver, subresource), |
| 120 | Draft::Draft201909 => { |
| 121 | draft201909::maybe_in_subresource(segments, resolver, subresource) |
| 122 | } |
| 123 | Draft::Draft202012 | Draft::Unknown => { |
| 124 | subresources::maybe_in_subresource(segments, resolver, subresource) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | /// Identifies known JSON schema keywords per draft. |
| 129 | #[must_use] |
| 130 | pub fn is_known_keyword(&self, keyword: &str) -> bool { |
| 131 | match keyword { |
| 132 | "$ref" |
| 133 | | "$schema" |
| 134 | | "additionalItems" |
| 135 | | "additionalProperties" |
| 136 | | "allOf" |
| 137 | | "anyOf" |
| 138 | | "dependencies" |
| 139 | | "enum" |
| 140 | | "exclusiveMaximum" |
| 141 | | "exclusiveMinimum" |
| 142 | | "format" |
| 143 | | "items" |
| 144 | | "maxItems" |
| 145 | | "maxLength" |
| 146 | | "maxProperties" |
| 147 | | "maximum" |
| 148 | | "minItems" |
| 149 | | "minLength" |
| 150 | | "minProperties" |
| 151 | | "minimum" |
| 152 | | "multipleOf" |
| 153 | | "not" |
| 154 | | "oneOf" |
| 155 | | "pattern" |
| 156 | | "patternProperties" |
| 157 | | "properties" |
| 158 | | "required" |
| 159 | | "type" |
| 160 | | "uniqueItems" => true, |
| 161 | |
| 162 | "id" if *self == Draft::Draft4 => true, |
| 163 | |
| 164 | "$id" | "const" | "contains" | "propertyNames" |
| 165 | if *self >= Draft::Draft6 || *self == Draft::Unknown => |
| 166 | { |
| 167 | true |
| 168 | } |
| 169 | |
| 170 | "contentEncoding" | "contentMediaType" |
| 171 | if matches!(self, Draft::Draft6 | Draft::Draft7) => |
| 172 | { |
| 173 | true |
| 174 | } |
| 175 | |
| 176 | "else" | "if" | "then" if *self >= Draft::Draft7 || *self == Draft::Unknown => true, |
| 177 | |
| 178 | "$anchor" |
| 179 | | "$defs" |
| 180 | | "$recursiveAnchor" |
| 181 | | "$recursiveRef" |
| 182 | | "dependentRequired" |
| 183 | | "dependentSchemas" |
| 184 | | "maxContains" |
| 185 | | "minContains" |
| 186 | | "prefixItems" |
| 187 | | "unevaluatedItems" |
| 188 | | "unevaluatedProperties" |
| 189 | if *self >= Draft::Draft201909 || *self == Draft::Unknown => |
| 190 | { |
| 191 | true |
| 192 | } |
| 193 | |
| 194 | "$dynamicAnchor" | "$dynamicRef" |
| 195 | if *self == Draft::Draft202012 || *self == Draft::Unknown => |
| 196 | { |
| 197 | true |
| 198 | } |
| 199 | |
| 200 | _ => false, |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | pub(crate) fn default_vocabularies(self) -> VocabularySet { |
| 205 | match self { |
| 206 | Draft::Draft4 | Draft::Draft6 | Draft::Draft7 => VocabularySet::new(), |
| 207 | Draft::Draft201909 => VocabularySet::from_known(DRAFT_2019_09_VOCABULARIES), |
| 208 | Draft::Draft202012 | Draft::Unknown => { |
| 209 | VocabularySet::from_known(DRAFT_2020_12_VOCABULARIES) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[cfg(test)] |
| 216 | mod tests { |
| 217 | use crate::Draft; |
| 218 | use serde_json::json; |
| 219 | use test_case::test_case; |
| 220 | |
| 221 | #[test_case(&json!({"$schema": "https://json-schema.org/draft/2020-12/schema"}), Draft::Draft202012; "detect Draft 2020-12")] |
| 222 | #[test_case(&json!({"$schema": "https://json-schema.org/draft/2020-12/schema#"}), Draft::Draft202012; "detect Draft 2020-12 with fragment")] |
| 223 | #[test_case(&json!({"$schema": "https://json-schema.org/draft/2019-09/schema"}), Draft::Draft201909; "detect Draft 2019-09")] |
| 224 | #[test_case(&json!({"$schema": "http://json-schema.org/draft-07/schema"}), Draft::Draft7; "detect Draft 7")] |
| 225 | #[test_case(&json!({"$schema": "https://json-schema.org/draft-07/schema"}), Draft::Draft7; "detect Draft 7 https")] |
| 226 | #[test_case(&json!({"$schema": "http://json-schema.org/draft-06/schema"}), Draft::Draft6; "detect Draft 6")] |
| 227 | #[test_case(&json!({"$schema": "https://json-schema.org/draft-06/schema"}), Draft::Draft6; "detect Draft 6 https")] |
| 228 | #[test_case(&json!({"$schema": "http://json-schema.org/draft-04/schema"}), Draft::Draft4; "detect Draft 4")] |
| 229 | #[test_case(&json!({"$schema": "https://json-schema.org/draft-04/schema"}), Draft::Draft4; "detect Draft 4 https")] |
| 230 | #[test_case(&json!({}), Draft::Draft7; "default to Draft 7 when no $schema")] |
| 231 | fn test_detect(contents: &serde_json::Value, expected: Draft) { |
| 232 | let result = Draft::Draft7.detect(contents); |
| 233 | assert_eq!(result, expected); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn test_unknown_specification() { |
| 238 | let draft = Draft::Draft7.detect(&json!({"$schema": "invalid"})); |
| 239 | assert_eq!(draft, Draft::Unknown); |
| 240 | } |
| 241 | |
| 242 | #[test_case(Draft::Draft4; "Draft 4 stays Draft 4")] |
| 243 | #[test_case(Draft::Draft6; "Draft 6 stays Draft 6")] |
| 244 | #[test_case(Draft::Draft7; "Draft 7 stays Draft 7")] |
| 245 | #[test_case(Draft::Draft201909; "Draft 2019-09 stays Draft 2019-09")] |
| 246 | #[test_case(Draft::Draft202012; "Draft 2020-12 stays Draft 2020-12")] |
| 247 | fn test_detect_no_change(draft: Draft) { |
| 248 | let contents = json!({}); |
| 249 | let result = draft.detect(&contents); |
| 250 | assert_eq!(result, draft); |
| 251 | } |
| 252 | } |