Skip to main content

jsonschema_schema/schema/vocabularies/
validation.rs

1use indexmap::IndexMap;
2use serde_json::{Number, Value};
3
4use crate::TypeValue;
5
6/// Validation vocabulary — type checks and numeric, string, array,
7/// and object constraints.
8///
9/// See [JSON Schema Validation §6](https://json-schema.org/draft/2020-12/json-schema-validation#section-6).
10#[derive(
11    Debug,
12    Clone,
13    Default,
14    PartialEq,
15    Eq,
16    serde::Serialize,
17    serde::Deserialize,
18    schemars::JsonSchema,
19    combine_structs::Fields,
20)]
21pub struct ValidationVocabulary {
22    // -- Type (§6.1) --
23    /// The `type` keyword — instance type constraint.
24    ///
25    /// The value of this keyword MUST be either a string or an array.
26    /// If it is an array, elements of the array MUST be strings and
27    /// MUST be unique.
28    ///
29    /// String values MUST be one of the six primitive types (`"null"`,
30    /// `"boolean"`, `"object"`, `"array"`, `"number"`, or `"string"`),
31    /// or `"integer"` which matches any number with a zero fractional
32    /// part.
33    ///
34    /// If the value of `"type"` is a string, then an instance validates
35    /// successfully if its type matches the type represented by the
36    /// value of the string. If the value of `"type"` is an array, then
37    /// an instance validates successfully if its type matches any of the
38    /// types indicated by the strings in the array.
39    ///
40    /// See [JSON Schema Validation §6.1.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.1).
41    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
42    pub type_: Option<TypeValue>,
43
44    /// The `enum` keyword — enumerated values constraint.
45    ///
46    /// The value of this keyword MUST be an array. This array SHOULD
47    /// have at least one element. Elements in the array SHOULD be
48    /// unique.
49    ///
50    /// An instance validates successfully against this keyword if its
51    /// value is equal to one of the elements in this keyword's array
52    /// value.
53    ///
54    /// Elements in the array might be of any type, including null.
55    ///
56    /// See [JSON Schema Validation §6.1.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.2).
57    #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
58    pub enum_: Option<Vec<Value>>,
59
60    /// The `const` keyword — constant value constraint.
61    ///
62    /// The value of this keyword MAY be of any type, including null.
63    ///
64    /// Use of this keyword is functionally equivalent to an `"enum"`
65    /// (Section 6.1.2) with a single value.
66    ///
67    /// An instance validates successfully against this keyword if its
68    /// value is equal to the value of the keyword.
69    ///
70    /// See [JSON Schema Validation §6.1.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.3).
71    #[serde(rename = "const", skip_serializing_if = "Option::is_none")]
72    pub const_: Option<Value>,
73
74    // -- Numeric (§6.2) --
75    /// The `minimum` keyword — inclusive lower bound.
76    ///
77    /// The value of `"minimum"` MUST be a number, representing an
78    /// inclusive lower limit for a numeric instance.
79    ///
80    /// If the instance is a number, then this keyword validates only if
81    /// the instance is greater than or exactly equal to `"minimum"`.
82    ///
83    /// See [JSON Schema Validation §6.2.4](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.4).
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub minimum: Option<Number>,
86
87    /// The `maximum` keyword — inclusive upper bound.
88    ///
89    /// The value of `"maximum"` MUST be a number, representing an
90    /// inclusive upper limit for a numeric instance.
91    ///
92    /// If the instance is a number, then this keyword validates only if
93    /// the instance is less than or exactly equal to `"maximum"`.
94    ///
95    /// See [JSON Schema Validation §6.2.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.2).
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub maximum: Option<Number>,
98
99    /// The `exclusiveMinimum` keyword — exclusive lower bound.
100    ///
101    /// The value of `"exclusiveMinimum"` MUST be a number, representing
102    /// an exclusive lower limit for a numeric instance.
103    ///
104    /// If the instance is a number, then the instance is valid only if
105    /// it has a value strictly greater than (not equal to)
106    /// `"exclusiveMinimum"`.
107    ///
108    /// See [JSON Schema Validation §6.2.5](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.5).
109    #[serde(rename = "exclusiveMinimum", skip_serializing_if = "Option::is_none")]
110    pub exclusive_minimum: Option<Number>,
111
112    /// The `exclusiveMaximum` keyword — exclusive upper bound.
113    ///
114    /// The value of `"exclusiveMaximum"` MUST be a number, representing
115    /// an exclusive upper limit for a numeric instance.
116    ///
117    /// If the instance is a number, then the instance is valid only if
118    /// it has a value strictly less than (not equal to)
119    /// `"exclusiveMaximum"`.
120    ///
121    /// See [JSON Schema Validation §6.2.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.3).
122    #[serde(rename = "exclusiveMaximum", skip_serializing_if = "Option::is_none")]
123    pub exclusive_maximum: Option<Number>,
124
125    /// The `multipleOf` keyword — divisibility constraint.
126    ///
127    /// The value of `"multipleOf"` MUST be a number, strictly greater
128    /// than 0.
129    ///
130    /// A numeric instance is valid only if division by this keyword's
131    /// value results in an integer.
132    ///
133    /// See [JSON Schema Validation §6.2.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2.1).
134    #[serde(rename = "multipleOf", skip_serializing_if = "Option::is_none")]
135    #[schemars(extend("exclusiveMinimum" = 0))]
136    pub multiple_of: Option<Number>,
137
138    // -- String (§6.3) --
139    /// The `minLength` keyword — minimum string length.
140    ///
141    /// The value of this keyword MUST be a non-negative integer.
142    ///
143    /// A string instance is valid against this keyword if its length is
144    /// greater than, or equal to, the value of this keyword.
145    ///
146    /// The length of a string instance is defined as the number of its
147    /// characters as defined by RFC 8259.
148    ///
149    /// Omitting this keyword has the same behavior as a value of 0.
150    ///
151    /// See [JSON Schema Validation §6.3.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3.2).
152    #[serde(rename = "minLength", skip_serializing_if = "Option::is_none")]
153    pub min_length: Option<u64>,
154
155    /// The `maxLength` keyword — maximum string length.
156    ///
157    /// The value of this keyword MUST be a non-negative integer.
158    ///
159    /// A string instance is valid against this keyword if its length is
160    /// less than, or equal to, the value of this keyword.
161    ///
162    /// The length of a string instance is defined as the number of its
163    /// characters as defined by RFC 8259.
164    ///
165    /// See [JSON Schema Validation §6.3.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3.1).
166    #[serde(rename = "maxLength", skip_serializing_if = "Option::is_none")]
167    pub max_length: Option<u64>,
168
169    /// The `pattern` keyword — regex constraint.
170    ///
171    /// The value of this keyword MUST be a string. This string SHOULD
172    /// be a valid regular expression, according to the ECMA-262
173    /// regular expression dialect.
174    ///
175    /// A string instance is considered valid if the regular expression
176    /// matches the instance successfully. Recall: regular expressions
177    /// are not implicitly anchored.
178    ///
179    /// See [JSON Schema Validation §6.3.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3.3).
180    #[serde(skip_serializing_if = "Option::is_none")]
181    #[schemars(extend("format" = "regex"))]
182    pub pattern: Option<String>,
183
184    // -- Array (§6.4) --
185    /// The `minItems` keyword — minimum array length.
186    ///
187    /// The value of this keyword MUST be a non-negative integer.
188    ///
189    /// An array instance is valid against `"minItems"` if its size is
190    /// greater than, or equal to, the value of this keyword.
191    ///
192    /// Omitting this keyword has the same behavior as a value of 0.
193    ///
194    /// See [JSON Schema Validation §6.4.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.2).
195    #[serde(rename = "minItems", skip_serializing_if = "Option::is_none")]
196    pub min_items: Option<u64>,
197
198    /// The `maxItems` keyword — maximum array length.
199    ///
200    /// The value of this keyword MUST be a non-negative integer.
201    ///
202    /// An array instance is valid against `"maxItems"` if its size is
203    /// less than, or equal to, the value of this keyword.
204    ///
205    /// See [JSON Schema Validation §6.4.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.1).
206    #[serde(rename = "maxItems", skip_serializing_if = "Option::is_none")]
207    pub max_items: Option<u64>,
208
209    /// The `uniqueItems` keyword — array element uniqueness.
210    ///
211    /// The value of this keyword MUST be a boolean.
212    ///
213    /// If this keyword has boolean value false, the instance validates
214    /// successfully. If it has boolean value true, the instance
215    /// validates successfully if all of its elements are unique.
216    ///
217    /// Omitting this keyword has the same behavior as a value of false.
218    ///
219    /// See [JSON Schema Validation §6.4.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.3).
220    #[serde(
221        default,
222        rename = "uniqueItems",
223        skip_serializing_if = "crate::schema::is_false"
224    )]
225    #[schemars(extend("default" = false))]
226    pub unique_items: bool,
227
228    /// The `minContains` keyword — minimum `contains` matches.
229    ///
230    /// The value of this keyword MUST be a non-negative integer.
231    ///
232    /// If `"contains"` is not present within the same schema object,
233    /// then this keyword has no effect.
234    ///
235    /// An instance array is valid against `"minContains"` in two ways,
236    /// depending on the form of the annotation result of an adjacent
237    /// `"contains"` keyword. The first way is if the annotation result
238    /// is an array and the length of that array is greater than or
239    /// equal to the `"minContains"` value. The second way is if the
240    /// annotation result is a boolean `true` and the instance array
241    /// length is greater than or equal to the `"minContains"` value.
242    ///
243    /// A value of 0 is allowed, but is only useful for setting a range
244    /// of occurrences from 0 to the value of `"maxContains"`. A value
245    /// of 0 causes `"minContains"` and `"contains"` to always pass
246    /// validation (but validation can still fail against a
247    /// `"maxContains"` keyword).
248    ///
249    /// Omitting this keyword has the same behavior as a value of 1.
250    ///
251    /// See [JSON Schema Validation §6.4.5](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.5).
252    #[serde(rename = "minContains", skip_serializing_if = "Option::is_none")]
253    #[schemars(extend("default" = 1))]
254    pub min_contains: Option<u64>,
255
256    /// The `maxContains` keyword — maximum `contains` matches.
257    ///
258    /// The value of this keyword MUST be a non-negative integer.
259    ///
260    /// If `"contains"` is not present within the same schema object,
261    /// then this keyword has no effect.
262    ///
263    /// An instance array is valid against `"maxContains"` in two ways,
264    /// depending on the form of the annotation result of an adjacent
265    /// `"contains"` keyword. The first way is if the annotation result
266    /// is an array and the length of that array is less than or equal
267    /// to the `"maxContains"` value. The second way is if the
268    /// annotation result is a boolean `true` and the instance array
269    /// length is less than or equal to the `"maxContains"` value.
270    ///
271    /// See [JSON Schema Validation §6.4.4](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4.4).
272    #[serde(rename = "maxContains", skip_serializing_if = "Option::is_none")]
273    pub max_contains: Option<u64>,
274
275    // -- Object (§6.5) --
276    /// The `required` keyword — required property names.
277    ///
278    /// The value of this keyword MUST be an array. Elements of this
279    /// array, if any, MUST be strings, and MUST be unique.
280    ///
281    /// An object instance is valid against this keyword if every item
282    /// in the array is the name of a property in the instance.
283    ///
284    /// Omitting this keyword has the same behavior as an empty array.
285    ///
286    /// See [JSON Schema Validation §6.5.3](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.3).
287    #[serde(skip_serializing_if = "Option::is_none")]
288    #[schemars(extend("uniqueItems" = true))]
289    pub required: Option<Vec<String>>,
290
291    /// The `minProperties` keyword — minimum property count.
292    ///
293    /// The value of this keyword MUST be a non-negative integer.
294    ///
295    /// An object instance is valid against `"minProperties"` if its
296    /// number of properties is greater than, or equal to, the value of
297    /// this keyword.
298    ///
299    /// Omitting this keyword has the same behavior as a value of 0.
300    ///
301    /// See [JSON Schema Validation §6.5.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.2).
302    #[serde(rename = "minProperties", skip_serializing_if = "Option::is_none")]
303    pub min_properties: Option<u64>,
304
305    /// The `maxProperties` keyword — maximum property count.
306    ///
307    /// The value of this keyword MUST be a non-negative integer.
308    ///
309    /// An object instance is valid against `"maxProperties"` if its
310    /// number of properties is less than, or equal to, the value of
311    /// this keyword.
312    ///
313    /// See [JSON Schema Validation §6.5.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.1).
314    #[serde(rename = "maxProperties", skip_serializing_if = "Option::is_none")]
315    pub max_properties: Option<u64>,
316
317    /// The `dependentRequired` keyword — conditional required
318    /// properties.
319    ///
320    /// The value of this keyword MUST be an object. Properties in this
321    /// object, if any, MUST be arrays. Elements in each array, if any,
322    /// MUST be strings, and MUST be unique.
323    ///
324    /// This keyword specifies properties that are required if a
325    /// specific other property is present. Their requirement is
326    /// dependent on the presence of the other property.
327    ///
328    /// Validation succeeds if, for each name that appears in both the
329    /// instance and as a name within this keyword's value, every item
330    /// in the corresponding array is also the name of a property in
331    /// the instance.
332    ///
333    /// Omitting this keyword has the same behavior as an empty object.
334    ///
335    /// See [JSON Schema Validation §6.5.4](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5.4).
336    #[serde(rename = "dependentRequired", skip_serializing_if = "Option::is_none")]
337    pub dependent_required: Option<IndexMap<String, Vec<String>>>,
338}