| 1 | use core::fmt; |
| 2 | use std::str::FromStr; |
| 3 | |
| 4 | use crate::{uri, Error}; |
| 5 | use ahash::AHashSet; |
| 6 | use fluent_uri::Uri; |
| 7 | use serde_json::Value; |
| 8 | |
| 9 | /// A JSON Schema vocabulary identifier, representing standard vocabularies (Core, Applicator, etc.) |
| 10 | /// or custom ones via URI. |
| 11 | #[derive(Debug, PartialEq, Eq, Clone)] |
| 12 | pub enum Vocabulary { |
| 13 | Core, |
| 14 | Applicator, |
| 15 | Unevaluated, |
| 16 | Validation, |
| 17 | Metadata, |
| 18 | Format, |
| 19 | FormatAnnotation, |
| 20 | Content, |
| 21 | Custom(Uri<String>), |
| 22 | } |
| 23 | |
| 24 | impl FromStr for Vocabulary { |
| 25 | type Err = Error; |
| 26 | |
| 27 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 28 | match s { |
| 29 | "https://json-schema.org/draft/2020-12/vocab/core" |
| 30 | | "https://json-schema.org/draft/2019-09/vocab/core" => Ok(Vocabulary::Core), |
| 31 | "https://json-schema.org/draft/2020-12/vocab/applicator" |
| 32 | | "https://json-schema.org/draft/2019-09/vocab/applicator" => { |
| 33 | Ok(Vocabulary::Applicator) |
| 34 | } |
| 35 | "https://json-schema.org/draft/2020-12/vocab/unevaluated" => { |
| 36 | Ok(Vocabulary::Unevaluated) |
| 37 | } |
| 38 | "https://json-schema.org/draft/2020-12/vocab/validation" |
| 39 | | "https://json-schema.org/draft/2019-09/vocab/validation" => { |
| 40 | Ok(Vocabulary::Validation) |
| 41 | } |
| 42 | "https://json-schema.org/draft/2020-12/vocab/meta-data" |
| 43 | | "https://json-schema.org/draft/2019-09/vocab/meta-data" => Ok(Vocabulary::Metadata), |
| 44 | "https://json-schema.org/draft/2020-12/vocab/format" |
| 45 | | "https://json-schema.org/draft/2019-09/vocab/format" => Ok(Vocabulary::Format), |
| 46 | "https://json-schema.org/draft/2020-12/vocab/format-annotation" => { |
| 47 | Ok(Vocabulary::FormatAnnotation) |
| 48 | } |
| 49 | "https://json-schema.org/draft/2020-12/vocab/content" |
| 50 | | "https://json-schema.org/draft/2019-09/vocab/content" => Ok(Vocabulary::Content), |
| 51 | _ => Ok(Vocabulary::Custom(uri::from_str(s)?)), |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// A set of enabled JSON Schema vocabularies. |
| 57 | #[derive(Clone, Default, PartialEq, Eq)] |
| 58 | pub struct VocabularySet { |
| 59 | known: u8, |
| 60 | custom: AHashSet<Uri<String>>, |
| 61 | } |
| 62 | |
| 63 | impl fmt::Debug for VocabularySet { |
| 64 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 | let mut debug_list = f.debug_list(); |
| 66 | |
| 67 | // Add known vocabularies |
| 68 | if self.known & (1 << 0) != 0 { |
| 69 | debug_list.entry(&"core"); |
| 70 | } |
| 71 | if self.known & (1 << 1) != 0 { |
| 72 | debug_list.entry(&"applicator"); |
| 73 | } |
| 74 | if self.known & (1 << 2) != 0 { |
| 75 | debug_list.entry(&"unevaluated"); |
| 76 | } |
| 77 | if self.known & (1 << 3) != 0 { |
| 78 | debug_list.entry(&"validation"); |
| 79 | } |
| 80 | if self.known & (1 << 4) != 0 { |
| 81 | debug_list.entry(&"meta-data"); |
| 82 | } |
| 83 | if self.known & (1 << 5) != 0 { |
| 84 | debug_list.entry(&"format"); |
| 85 | } |
| 86 | if self.known & (1 << 6) != 0 { |
| 87 | debug_list.entry(&"format-annotation"); |
| 88 | } |
| 89 | if self.known & (1 << 7) != 0 { |
| 90 | debug_list.entry(&"content"); |
| 91 | } |
| 92 | |
| 93 | // Add custom vocabularies |
| 94 | if !self.custom.is_empty() { |
| 95 | let mut custom: Vec<_> = self.custom.iter().map(Uri::as_str).collect(); |
| 96 | custom.sort_unstable(); |
| 97 | for uri in custom { |
| 98 | debug_list.entry(&uri); |
| 99 | } |
| 100 | } |
| 101 | debug_list.finish() |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl VocabularySet { |
| 106 | pub(crate) fn new() -> Self { |
| 107 | Self::default() |
| 108 | } |
| 109 | |
| 110 | pub(crate) fn from_known(known: u8) -> Self { |
| 111 | Self { |
| 112 | known, |
| 113 | custom: AHashSet::new(), |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub(crate) fn add(&mut self, vocabulary: Vocabulary) { |
| 118 | match vocabulary { |
| 119 | Vocabulary::Core => self.known |= 1 << 0, |
| 120 | Vocabulary::Applicator => self.known |= 1 << 1, |
| 121 | Vocabulary::Unevaluated => self.known |= 1 << 2, |
| 122 | Vocabulary::Validation => self.known |= 1 << 3, |
| 123 | Vocabulary::Metadata => self.known |= 1 << 4, |
| 124 | Vocabulary::Format => self.known |= 1 << 5, |
| 125 | Vocabulary::FormatAnnotation => self.known |= 1 << 6, |
| 126 | Vocabulary::Content => self.known |= 1 << 7, |
| 127 | Vocabulary::Custom(uri) => { |
| 128 | self.custom.insert(uri); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | #[must_use] |
| 133 | pub fn contains(&self, vocabulary: &Vocabulary) -> bool { |
| 134 | match vocabulary { |
| 135 | Vocabulary::Core => self.known & (1 << 0) != 0, |
| 136 | Vocabulary::Applicator => self.known & (1 << 1) != 0, |
| 137 | Vocabulary::Unevaluated => self.known & (1 << 2) != 0, |
| 138 | Vocabulary::Validation => self.known & (1 << 3) != 0, |
| 139 | Vocabulary::Metadata => self.known & (1 << 4) != 0, |
| 140 | Vocabulary::Format => self.known & (1 << 5) != 0, |
| 141 | Vocabulary::FormatAnnotation => self.known & (1 << 6) != 0, |
| 142 | Vocabulary::Content => self.known & (1 << 7) != 0, |
| 143 | Vocabulary::Custom(uri) => self.custom.contains(uri), |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | pub(crate) const DRAFT_2020_12_VOCABULARIES: u8 = 0b1111_1111; |
| 149 | pub(crate) const DRAFT_2019_09_VOCABULARIES: u8 = 0b1001_1011; |
| 150 | |
| 151 | pub(crate) fn find(document: &Value) -> Result<Option<VocabularySet>, Error> { |
| 152 | if let Some(schema) = document.get("$id").and_then(|s| s.as_str()) { |
| 153 | match schema { |
| 154 | "https://json-schema.org/schema" | "https://json-schema.org/draft/2020-12/schema" => { |
| 155 | // All known vocabularies |
| 156 | Ok(Some(VocabularySet::from_known(DRAFT_2020_12_VOCABULARIES))) |
| 157 | } |
| 158 | "https://json-schema.org/draft/2019-09/schema" => { |
| 159 | // Core, Applicator, Validation, Metadata, Content |
| 160 | Ok(Some(VocabularySet::from_known(DRAFT_2019_09_VOCABULARIES))) |
| 161 | } |
| 162 | "https://json-schema.org/draft-07/schema" |
| 163 | | "https://json-schema.org/draft-06/schema" |
| 164 | | "https://json-schema.org/draft-04/schema" => Ok(None), |
| 165 | _ => { |
| 166 | // For unknown schemas, parse the $vocabulary object |
| 167 | if let Some(vocab_obj) = document.get("$vocabulary").and_then(|v| v.as_object()) { |
| 168 | let mut set = VocabularySet::new(); |
| 169 | for (uri, enabled) in vocab_obj { |
| 170 | if enabled.as_bool().unwrap_or(false) { |
| 171 | set.add(Vocabulary::from_str(uri)?); |
| 172 | } |
| 173 | } |
| 174 | Ok(Some(set)) |
| 175 | } else { |
| 176 | Ok(None) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } else { |
| 181 | Ok(None) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | #[cfg(test)] |
| 186 | mod tests { |
| 187 | use super::*; |
| 188 | use test_case::test_case; |
| 189 | |
| 190 | #[test_case(&Vocabulary::Core, 0b0000_0001, true)] |
| 191 | #[test_case(&Vocabulary::Applicator, 0b0000_0010, true)] |
| 192 | #[test_case(&Vocabulary::Unevaluated, 0b0000_0100, true)] |
| 193 | #[test_case(&Vocabulary::Validation, 0b0000_1000, true)] |
| 194 | #[test_case(&Vocabulary::Metadata, 0b0001_0000, true)] |
| 195 | #[test_case(&Vocabulary::Format, 0b0010_0000, true)] |
| 196 | #[test_case(&Vocabulary::FormatAnnotation, 0b0100_0000, true)] |
| 197 | #[test_case(&Vocabulary::Content, 0b1000_0000, true)] |
| 198 | #[test_case(&Vocabulary::Core, 0b1111_1110, false)] |
| 199 | #[test_case(&Vocabulary::Applicator, 0b1111_1101, false)] |
| 200 | #[test_case(&Vocabulary::Unevaluated, 0b111_11011, false)] |
| 201 | #[test_case(&Vocabulary::Validation, 0b1111_0111, false)] |
| 202 | #[test_case(&Vocabulary::Metadata, 0b1110_1111, false)] |
| 203 | #[test_case(&Vocabulary::Format, 0b1101_1111, false)] |
| 204 | #[test_case(&Vocabulary::FormatAnnotation, 0b1011_1111, false)] |
| 205 | #[test_case(&Vocabulary::Content, 0b0111_1111, false)] |
| 206 | fn test_vocabulary_set(vocabulary: &Vocabulary, known: u8, expected: bool) { |
| 207 | let set = VocabularySet::from_known(known); |
| 208 | assert_eq!(set.contains(vocabulary), expected); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn test_vocabulary_set_add_and_contains() { |
| 213 | let mut set = VocabularySet::new(); |
| 214 | |
| 215 | set.add(Vocabulary::Core); |
| 216 | set.add(Vocabulary::Applicator); |
| 217 | set.add(Vocabulary::Validation); |
| 218 | set.add(Vocabulary::Metadata); |
| 219 | set.add(Vocabulary::Content); |
| 220 | |
| 221 | assert!(set.contains(&Vocabulary::Core)); |
| 222 | assert!(set.contains(&Vocabulary::Applicator)); |
| 223 | assert!(set.contains(&Vocabulary::Validation)); |
| 224 | assert!(set.contains(&Vocabulary::Metadata)); |
| 225 | assert!(set.contains(&Vocabulary::Content)); |
| 226 | |
| 227 | assert!(!set.contains(&Vocabulary::Unevaluated)); |
| 228 | assert!(!set.contains(&Vocabulary::Format)); |
| 229 | assert!(!set.contains(&Vocabulary::FormatAnnotation)); |
| 230 | |
| 231 | set.add(Vocabulary::Unevaluated); |
| 232 | set.add(Vocabulary::Format); |
| 233 | set.add(Vocabulary::FormatAnnotation); |
| 234 | |
| 235 | assert!(set.contains(&Vocabulary::Unevaluated)); |
| 236 | assert!(set.contains(&Vocabulary::Format)); |
| 237 | assert!(set.contains(&Vocabulary::FormatAnnotation)); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn test_vocabulary_set_debug() { |
| 242 | let mut set = VocabularySet::from_known(0b0001_1111); // Core, Applicator, Unevaluated, Validation, Metadata |
| 243 | set.add(Vocabulary::Custom( |
| 244 | uri::from_str("https://example.com/custom-vocab").unwrap(), |
| 245 | )); |
| 246 | |
| 247 | assert_eq!( |
| 248 | format!("{set:?}"), |
| 249 | "[\"core\", \"applicator\", \"unevaluated\", \"validation\", \"meta-data\", \"https://example.com/custom-vocab\"]" |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn test_custom_vocabulary() { |
| 255 | let custom_uri = uri::from_str("https://example.com/custom-vocab").expect("Invalid URI"); |
| 256 | let mut set = VocabularySet::new(); |
| 257 | set.add(Vocabulary::Custom(custom_uri.clone())); |
| 258 | |
| 259 | assert!(set.contains(&Vocabulary::Custom(custom_uri))); |
| 260 | assert!(!set.contains(&Vocabulary::Custom( |
| 261 | uri::from_str("https://example.com/other-vocab").expect("Invalid URI") |
| 262 | ))); |
| 263 | } |
| 264 | |
| 265 | #[test_case( |
| 266 | &serde_json::json!({"$id": "https://json-schema.org/draft/2020-12/schema"}), |
| 267 | "Some([\"core\", \"applicator\", \"unevaluated\", \"validation\", \"meta-data\", \"format\", \"format-annotation\", \"content\"])" |
| 268 | ; "2020-12 draft" |
| 269 | )] |
| 270 | #[test_case( |
| 271 | &serde_json::json!({"$id": "https://json-schema.org/draft/2019-09/schema"}), |
| 272 | "Some([\"core\", \"applicator\", \"validation\", \"meta-data\", \"content\"])" |
| 273 | ; "2019-09 draft" |
| 274 | )] |
| 275 | #[test_case( |
| 276 | &serde_json::json!({"$id": "https://json-schema.org/draft-07/schema"}), |
| 277 | "None" |
| 278 | ; "draft-07" |
| 279 | )] |
| 280 | #[test_case( |
| 281 | &serde_json::json!({ |
| 282 | "$id": "https://example.com/custom-schema", |
| 283 | "$vocabulary": { |
| 284 | "https://example.com/custom-vocab1": true, |
| 285 | "https://example.com/custom-vocab2": true, |
| 286 | "https://example.com/custom-vocab3": false, |
| 287 | } |
| 288 | }), |
| 289 | "Some([\"https://example.com/custom-vocab1\", \"https://example.com/custom-vocab2\"])" |
| 290 | ; "custom schema" |
| 291 | )] |
| 292 | #[test_case( |
| 293 | &serde_json::json!({}), |
| 294 | "None" |
| 295 | ; "no $id keyword" |
| 296 | )] |
| 297 | fn test_find(schema: &serde_json::Value, expected: &str) { |
| 298 | let set = find(schema).expect("Invalid vocabulary"); |
| 299 | assert_eq!(format!("{set:?}"), expected); |
| 300 | } |
| 301 | } |