| 1 | //! Built-in JSON Schema meta-schemas. |
| 2 | //! |
| 3 | //! This module provides access to the official JSON Schema meta-schemas for different draft versions. |
| 4 | use serde_json::Value; |
| 5 | use std::sync::{Arc, LazyLock}; |
| 6 | |
| 7 | use crate::Draft; |
| 8 | |
| 9 | macro_rules! schema { |
| 10 | ($vis:vis $name:ident, $path:expr) => { |
| 11 | $vis static $name: LazyLock<Arc<serde_json::Value>> = LazyLock::new(|| { |
| 12 | Arc::new(serde_json::from_slice(include_bytes!($path)).expect("Invalid schema")) |
| 13 | }); |
| 14 | }; |
| 15 | ($name:ident, $path:expr) => { |
| 16 | schema!(pub(crate) $name, $path); |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | schema!(pub DRAFT4, "../metaschemas/draft4.json"); |
| 21 | schema!(pub DRAFT6, "../metaschemas/draft6.json"); |
| 22 | schema!(pub DRAFT7, "../metaschemas/draft7.json"); |
| 23 | schema!(pub DRAFT201909, "../metaschemas/draft2019-09/schema.json"); |
| 24 | schema!( |
| 25 | pub DRAFT201909_APPLICATOR, |
| 26 | "../metaschemas/draft2019-09/meta/applicator.json" |
| 27 | ); |
| 28 | schema!( |
| 29 | pub DRAFT201909_CONTENT, |
| 30 | "../metaschemas/draft2019-09/meta/content.json" |
| 31 | ); |
| 32 | schema!( |
| 33 | pub DRAFT201909_CORE, |
| 34 | "../metaschemas/draft2019-09/meta/core.json" |
| 35 | ); |
| 36 | schema!( |
| 37 | pub DRAFT201909_FORMAT, |
| 38 | "../metaschemas/draft2019-09/meta/format.json" |
| 39 | ); |
| 40 | schema!( |
| 41 | pub DRAFT201909_META_DATA, |
| 42 | "../metaschemas/draft2019-09/meta/meta-data.json" |
| 43 | ); |
| 44 | schema!( |
| 45 | pub DRAFT201909_VALIDATION, |
| 46 | "../metaschemas/draft2019-09/meta/validation.json" |
| 47 | ); |
| 48 | schema!(pub DRAFT202012, "../metaschemas/draft2020-12/schema.json"); |
| 49 | schema!( |
| 50 | pub DRAFT202012_CORE, |
| 51 | "../metaschemas/draft2020-12/meta/core.json" |
| 52 | ); |
| 53 | schema!( |
| 54 | pub DRAFT202012_APPLICATOR, |
| 55 | "../metaschemas/draft2020-12/meta/applicator.json" |
| 56 | ); |
| 57 | schema!( |
| 58 | pub DRAFT202012_UNEVALUATED, |
| 59 | "../metaschemas/draft2020-12/meta/unevaluated.json" |
| 60 | ); |
| 61 | schema!( |
| 62 | pub DRAFT202012_VALIDATION, |
| 63 | "../metaschemas/draft2020-12/meta/validation.json" |
| 64 | ); |
| 65 | schema!( |
| 66 | pub DRAFT202012_META_DATA, |
| 67 | "../metaschemas/draft2020-12/meta/meta-data.json" |
| 68 | ); |
| 69 | schema!( |
| 70 | pub DRAFT202012_FORMAT_ANNOTATION, |
| 71 | "../metaschemas/draft2020-12/meta/format-annotation.json" |
| 72 | ); |
| 73 | schema!( |
| 74 | pub DRAFT202012_FORMAT_ASSERTION, |
| 75 | "../metaschemas/draft2020-12/meta/format-assertion.json" |
| 76 | ); |
| 77 | schema!( |
| 78 | pub DRAFT202012_CONTENT, |
| 79 | "../metaschemas/draft2020-12/meta/content.json" |
| 80 | ); |
| 81 | pub(crate) static META_SCHEMAS_ALL: LazyLock<[(&'static str, &'static Value); 18]> = |
| 82 | LazyLock::new(|| { |
| 83 | [ |
| 84 | ("http://json-schema.org/draft-04/schema#", &*DRAFT4), |
| 85 | ("http://json-schema.org/draft-06/schema#", &*DRAFT6), |
| 86 | ("http://json-schema.org/draft-07/schema#", &*DRAFT7), |
| 87 | ( |
| 88 | "https://json-schema.org/draft/2019-09/schema", |
| 89 | &*DRAFT201909, |
| 90 | ), |
| 91 | ( |
| 92 | "https://json-schema.org/draft/2019-09/meta/applicator", |
| 93 | &*DRAFT201909_APPLICATOR, |
| 94 | ), |
| 95 | ( |
| 96 | "https://json-schema.org/draft/2019-09/meta/content", |
| 97 | &*DRAFT201909_CONTENT, |
| 98 | ), |
| 99 | ( |
| 100 | "https://json-schema.org/draft/2019-09/meta/core", |
| 101 | &*DRAFT201909_CORE, |
| 102 | ), |
| 103 | ( |
| 104 | "https://json-schema.org/draft/2019-09/meta/format", |
| 105 | &*DRAFT201909_FORMAT, |
| 106 | ), |
| 107 | ( |
| 108 | "https://json-schema.org/draft/2019-09/meta/meta-data", |
| 109 | &*DRAFT201909_META_DATA, |
| 110 | ), |
| 111 | ( |
| 112 | "https://json-schema.org/draft/2019-09/meta/validation", |
| 113 | &*DRAFT201909_VALIDATION, |
| 114 | ), |
| 115 | ( |
| 116 | "https://json-schema.org/draft/2020-12/schema", |
| 117 | &*DRAFT202012, |
| 118 | ), |
| 119 | ( |
| 120 | "https://json-schema.org/draft/2020-12/meta/core", |
| 121 | &*DRAFT202012_CORE, |
| 122 | ), |
| 123 | ( |
| 124 | "https://json-schema.org/draft/2020-12/meta/applicator", |
| 125 | &*DRAFT202012_APPLICATOR, |
| 126 | ), |
| 127 | ( |
| 128 | "https://json-schema.org/draft/2020-12/meta/unevaluated", |
| 129 | &*DRAFT202012_UNEVALUATED, |
| 130 | ), |
| 131 | ( |
| 132 | "https://json-schema.org/draft/2020-12/meta/validation", |
| 133 | &*DRAFT202012_VALIDATION, |
| 134 | ), |
| 135 | ( |
| 136 | "https://json-schema.org/draft/2020-12/meta/meta-data", |
| 137 | &*DRAFT202012_META_DATA, |
| 138 | ), |
| 139 | ( |
| 140 | "https://json-schema.org/draft/2020-12/meta/format-annotation", |
| 141 | &*DRAFT202012_FORMAT_ANNOTATION, |
| 142 | ), |
| 143 | ( |
| 144 | "https://json-schema.org/draft/2020-12/meta/content", |
| 145 | &*DRAFT202012_CONTENT, |
| 146 | ), |
| 147 | ] |
| 148 | }); |
| 149 | |
| 150 | pub(crate) static META_SCHEMAS_DRAFT4: LazyLock<[(&'static str, &'static Value); 1]> = |
| 151 | LazyLock::new(|| [("http://json-schema.org/draft-04/schema#", &*DRAFT4)]); |
| 152 | |
| 153 | pub(crate) static META_SCHEMAS_DRAFT6: LazyLock<[(&'static str, &'static Value); 1]> = |
| 154 | LazyLock::new(|| [("http://json-schema.org/draft-06/schema#", &*DRAFT6)]); |
| 155 | |
| 156 | pub(crate) static META_SCHEMAS_DRAFT7: LazyLock<[(&'static str, &'static Value); 1]> = |
| 157 | LazyLock::new(|| [("http://json-schema.org/draft-07/schema#", &*DRAFT7)]); |
| 158 | |
| 159 | pub(crate) static META_SCHEMAS_DRAFT2019: LazyLock<[(&'static str, &'static Value); 7]> = |
| 160 | LazyLock::new(|| { |
| 161 | [ |
| 162 | ( |
| 163 | "https://json-schema.org/draft/2019-09/schema", |
| 164 | &*DRAFT201909, |
| 165 | ), |
| 166 | ( |
| 167 | "https://json-schema.org/draft/2019-09/meta/applicator", |
| 168 | &*DRAFT201909_APPLICATOR, |
| 169 | ), |
| 170 | ( |
| 171 | "https://json-schema.org/draft/2019-09/meta/content", |
| 172 | &*DRAFT201909_CONTENT, |
| 173 | ), |
| 174 | ( |
| 175 | "https://json-schema.org/draft/2019-09/meta/core", |
| 176 | &*DRAFT201909_CORE, |
| 177 | ), |
| 178 | ( |
| 179 | "https://json-schema.org/draft/2019-09/meta/format", |
| 180 | &*DRAFT201909_FORMAT, |
| 181 | ), |
| 182 | ( |
| 183 | "https://json-schema.org/draft/2019-09/meta/meta-data", |
| 184 | &*DRAFT201909_META_DATA, |
| 185 | ), |
| 186 | ( |
| 187 | "https://json-schema.org/draft/2019-09/meta/validation", |
| 188 | &*DRAFT201909_VALIDATION, |
| 189 | ), |
| 190 | ] |
| 191 | }); |
| 192 | |
| 193 | pub(crate) static META_SCHEMAS_DRAFT2020: LazyLock<[(&'static str, &'static Value); 8]> = |
| 194 | LazyLock::new(|| { |
| 195 | [ |
| 196 | ( |
| 197 | "https://json-schema.org/draft/2020-12/schema", |
| 198 | &*DRAFT202012, |
| 199 | ), |
| 200 | ( |
| 201 | "https://json-schema.org/draft/2020-12/meta/core", |
| 202 | &*DRAFT202012_CORE, |
| 203 | ), |
| 204 | ( |
| 205 | "https://json-schema.org/draft/2020-12/meta/applicator", |
| 206 | &*DRAFT202012_APPLICATOR, |
| 207 | ), |
| 208 | ( |
| 209 | "https://json-schema.org/draft/2020-12/meta/unevaluated", |
| 210 | &*DRAFT202012_UNEVALUATED, |
| 211 | ), |
| 212 | ( |
| 213 | "https://json-schema.org/draft/2020-12/meta/validation", |
| 214 | &*DRAFT202012_VALIDATION, |
| 215 | ), |
| 216 | ( |
| 217 | "https://json-schema.org/draft/2020-12/meta/meta-data", |
| 218 | &*DRAFT202012_META_DATA, |
| 219 | ), |
| 220 | ( |
| 221 | "https://json-schema.org/draft/2020-12/meta/format-annotation", |
| 222 | &*DRAFT202012_FORMAT_ANNOTATION, |
| 223 | ), |
| 224 | ( |
| 225 | "https://json-schema.org/draft/2020-12/meta/content", |
| 226 | &*DRAFT202012_CONTENT, |
| 227 | ), |
| 228 | ] |
| 229 | }); |
| 230 | |
| 231 | /// Return all the meta-schemas which are part of a given draft. |
| 232 | pub(crate) fn metas_for_draft(draft: Draft) -> &'static [(&'static str, &'static Value)] { |
| 233 | match draft { |
| 234 | Draft::Draft4 => &*META_SCHEMAS_DRAFT4, |
| 235 | Draft::Draft6 => &*META_SCHEMAS_DRAFT6, |
| 236 | Draft::Draft7 => &*META_SCHEMAS_DRAFT7, |
| 237 | Draft::Draft201909 => &*META_SCHEMAS_DRAFT2019, |
| 238 | // Unknown drafts default to 2020-12 vocabularies. |
| 239 | // Custom meta-schemas should explicitly declare vocabularies in their $vocabulary field. |
| 240 | Draft::Draft202012 | Draft::Unknown => &*META_SCHEMAS_DRAFT2020, |
| 241 | } |
| 242 | } |