Skip to main content

jsonschema_schema/schema/vocabularies/
core.rs

1use alloc::collections::BTreeMap;
2
3use indexmap::IndexMap;
4use url::Url;
5
6use crate::SchemaValue;
7
8/// Core vocabulary — identifiers, references, and definitions.
9///
10/// See [JSON Schema Core §8](https://json-schema.org/draft/2020-12/json-schema-core#section-8).
11#[derive(
12    Debug,
13    Clone,
14    Default,
15    PartialEq,
16    Eq,
17    serde::Serialize,
18    serde::Deserialize,
19    schemars::JsonSchema,
20    combine_structs::Fields,
21)]
22pub struct CoreVocabulary {
23    /// The `$schema` keyword — JSON Schema dialect identifier.
24    ///
25    /// The `"$schema"` keyword is both used as a JSON Schema dialect
26    /// identifier and as the identifier of a resource which is itself a
27    /// JSON Schema, which describes the set of valid schemas written
28    /// for this particular dialect.
29    ///
30    /// The value of this keyword MUST be a URI (containing a scheme)
31    /// and this URI MUST be normalized. The current schema MUST be
32    /// valid against the meta-schema identified by this URI.
33    ///
34    /// If this URI identifies a retrievable resource, that resource
35    /// SHOULD be of media type `"application/schema+json"`.
36    ///
37    /// The `"$schema"` keyword SHOULD be used in the document root
38    /// schema object, and MAY be used in the root schema objects of
39    /// embedded schema resources. It MUST NOT appear in non-resource
40    /// root schema objects. If absent from the document root schema,
41    /// the resulting behavior is implementation-defined.
42    ///
43    /// Values for this property are defined elsewhere in this and
44    /// other documents, and by other parties.
45    ///
46    /// See [JSON Schema Core §8.1.1](https://json-schema.org/draft/2020-12/json-schema-core#section-8.1.1).
47    #[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
48    pub schema: Option<Url>,
49
50    /// The `$id` keyword — schema resource identifier.
51    ///
52    /// The `"$id"` keyword identifies a schema resource with its
53    /// canonical URI.
54    ///
55    /// Note that this URI is an identifier and not necessarily a
56    /// network locator. In the case of a network-addressable URL, a
57    /// schema need not be downloadable from its canonical URI.
58    ///
59    /// If present, the value for this keyword MUST be a string, and
60    /// MUST represent a valid URI-reference. This URI-reference SHOULD
61    /// be normalized, and MUST resolve to an absolute-URI (without a
62    /// fragment), or to a URI with an empty fragment.
63    ///
64    /// The absolute-URI also serves as the base URI for relative
65    /// URI-references in keywords within the schema resource, in
66    /// accordance with RFC 3986 section 5.1.1 regarding base URIs
67    /// embedded in content.
68    ///
69    /// The presence of `"$id"` in a subschema indicates that the
70    /// subschema constitutes a distinct schema resource within a
71    /// single schema document.
72    ///
73    /// See [JSON Schema Core §8.2.1](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.1).
74    #[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
75    #[schemars(extend("format" = "uri-reference", "pattern" = "^[^#]*#?$"))]
76    pub id: Option<String>,
77
78    /// The `$ref` keyword — static schema reference.
79    ///
80    /// The `"$ref"` keyword is an applicator that is used to reference
81    /// a statically identified schema. Its results are the results of
82    /// the referenced schema. Note that this definition of how the
83    /// results are determined means that other keywords can appear
84    /// alongside of `"$ref"` in the same schema object.
85    ///
86    /// The value of the `"$ref"` keyword MUST be a string which is a
87    /// URI-Reference. Resolved against the current URI base, it
88    /// produces the URI of the schema to apply. This resolution is
89    /// safe to perform on schema load, as the process of evaluating an
90    /// instance cannot change how the reference resolves.
91    ///
92    /// See [JSON Schema Core §8.2.3.1](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3.1).
93    #[serde(rename = "$ref", skip_serializing_if = "Option::is_none")]
94    #[schemars(extend("format" = "uri-reference"))]
95    pub ref_: Option<String>,
96
97    /// The `$anchor` keyword — plain-name fragment identifier.
98    ///
99    /// The `"$anchor"` and `"$dynamicAnchor"` keywords are used to
100    /// specify plain name fragments. They are identifier keywords that
101    /// can only be used to create plain name fragments, rather than
102    /// absolute URIs as seen with `"$id"`.
103    ///
104    /// If present, the value of this keyword MUST be a string and MUST
105    /// start with a letter (`[A-Za-z]`) or underscore (`"_"`),
106    /// followed by any number of letters, digits (`[0-9]`), hyphens
107    /// (`"-"`), underscores (`"_"`), and periods (`"."`).
108    ///
109    /// See [JSON Schema Core §8.2.2](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.2).
110    #[serde(rename = "$anchor", skip_serializing_if = "Option::is_none")]
111    #[schemars(regex(pattern = r"^[A-Za-z_][-A-Za-z0-9._]*$"))]
112    pub anchor: Option<String>,
113
114    /// The `$dynamicRef` keyword — dynamic schema reference.
115    ///
116    /// The `"$dynamicRef"` keyword is an applicator that allows for
117    /// deferring the full resolution until runtime, at which point it
118    /// is resolved each time it is encountered while evaluating an
119    /// instance.
120    ///
121    /// Together with `"$dynamicAnchor"`, `"$dynamicRef"` implements a
122    /// cooperative extension mechanism that is primarily useful with
123    /// recursive schemas (schemas that reference themselves). Both the
124    /// extension point and the runtime-determined extension target are
125    /// defined with `"$dynamicAnchor"`, and only exhibit runtime
126    /// dynamic behavior when referenced with `"$dynamicRef"`.
127    ///
128    /// The value of the `"$dynamicRef"` property MUST be a string
129    /// which is a URI-Reference. Resolved against the current URI
130    /// base, it produces the URI used as the starting point for
131    /// runtime resolution.
132    ///
133    /// See [JSON Schema Core §8.2.3.2](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3.2).
134    #[serde(rename = "$dynamicRef", skip_serializing_if = "Option::is_none")]
135    #[schemars(extend("format" = "uri-reference"))]
136    pub dynamic_ref: Option<String>,
137
138    /// The `$dynamicAnchor` keyword — dynamic extension point.
139    ///
140    /// Separately from the usual usage of URIs, `"$dynamicAnchor"`
141    /// indicates that the fragment is an extension point when used
142    /// with the `"$dynamicRef"` keyword. This low-level, advanced
143    /// feature makes it easier to extend recursive schemas such as the
144    /// meta-schemas, without imposing any particular semantics on that
145    /// extension.
146    ///
147    /// If present, the value of this keyword MUST be a string and MUST
148    /// conform to the same rules as `"$anchor"`.
149    ///
150    /// See [JSON Schema Core §8.2.2](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.2).
151    #[serde(rename = "$dynamicAnchor", skip_serializing_if = "Option::is_none")]
152    #[schemars(regex(pattern = r"^[A-Za-z_][-A-Za-z0-9._]*$"))]
153    pub dynamic_anchor: Option<String>,
154
155    /// The `$comment` keyword — schema author comments.
156    ///
157    /// This keyword reserves a location for comments from schema
158    /// authors to readers or maintainers of the schema.
159    ///
160    /// The value of this keyword MUST be a string. Implementations
161    /// MUST NOT present this string to end users. Tools for editing
162    /// schemas SHOULD support displaying and editing this keyword. The
163    /// value of this keyword MAY be used in debug or error output
164    /// which is intended for developers making use of schemas.
165    ///
166    /// Implementations MAY strip `"$comment"` values at any point
167    /// during processing. In particular, this allows for shortening
168    /// schemas when the size of deployed schemas is a concern.
169    ///
170    /// Implementations MUST NOT take any other action based on the
171    /// presence, absence, or contents of `"$comment"` properties. In
172    /// particular, the value of `"$comment"` MUST NOT be collected as
173    /// an annotation result.
174    ///
175    /// See [JSON Schema Core §8.3](https://json-schema.org/draft/2020-12/json-schema-core#section-8.3).
176    #[serde(rename = "$comment", skip_serializing_if = "Option::is_none")]
177    pub comment: Option<String>,
178
179    /// The `$defs` keyword — inline re-usable schema definitions.
180    ///
181    /// The `$defs` keyword reserves a location for schema authors to
182    /// inline re-usable JSON Schemas into a more general schema. The
183    /// keyword does not directly affect the validation result.
184    ///
185    /// This keyword's value MUST be an object. Each member value of
186    /// this object MUST be a valid JSON Schema.
187    ///
188    /// As an example, here is a schema describing an array of positive
189    /// integers, where the positive integer constraint is a subschema in
190    /// `$defs`:
191    ///
192    /// ```json
193    /// {
194    ///     "type": "array",
195    ///     "items": { "$ref": "#/$defs/positiveInteger" },
196    ///     "$defs": {
197    ///         "positiveInteger": {
198    ///             "type": "integer",
199    ///             "exclusiveMinimum": 0
200    ///         }
201    ///     }
202    /// }
203    /// ```
204    ///
205    /// See [JSON Schema Core §8.2.4](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.4).
206    #[serde(rename = "$defs", skip_serializing_if = "Option::is_none")]
207    pub defs: Option<BTreeMap<String, SchemaValue>>,
208
209    /// The `$vocabulary` keyword — meta-schema vocabulary declaration.
210    ///
211    /// The `"$vocabulary"` keyword is used in meta-schemas to identify
212    /// the vocabularies available for use in schemas described by that
213    /// meta-schema. It is also used to indicate whether each vocabulary
214    /// is required or optional, in the sense that an implementation
215    /// MUST understand the required vocabularies in order to
216    /// successfully process the schema. Together, this information
217    /// forms a dialect. Any vocabulary that is understood by the
218    /// implementation MUST be processed in a manner consistent with
219    /// the semantic definitions contained within the vocabulary.
220    ///
221    /// The value of this keyword MUST be an object. The property names
222    /// in the object MUST be URIs (containing a scheme) and this URI
223    /// MUST be normalized. Each URI that appears as a property name
224    /// identifies a specific set of keywords and their semantics.
225    ///
226    /// The values of the object properties MUST be booleans. If the
227    /// value is true, then implementations that do not recognize the
228    /// vocabulary MUST refuse to process any schemas that declare this
229    /// meta-schema with `"$schema"`. If the value is false,
230    /// implementations that do not recognize the vocabulary SHOULD
231    /// proceed with processing such schemas. The value has no impact
232    /// if the implementation understands the vocabulary.
233    ///
234    /// The `"$vocabulary"` keyword SHOULD be used in the root schema
235    /// of any schema document intended for use as a meta-schema. It
236    /// MUST NOT appear in subschemas.
237    ///
238    /// The `"$vocabulary"` keyword MUST be ignored in schema documents
239    /// that are not being processed as a meta-schema.
240    ///
241    /// See [JSON Schema Core §8.1.2](https://json-schema.org/draft/2020-12/json-schema-core#section-8.1.2).
242    #[serde(rename = "$vocabulary", skip_serializing_if = "Option::is_none")]
243    #[schemars(extend("propertyNames" = { "format": "uri" }))]
244    pub vocabulary: Option<IndexMap<Url, bool>>,
245}