jsonschema 0.49.6

JSON schema validaton library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::collections::BTreeMap;

use serde_json::{Number, Value};

use crate::{
    canonical::{
        ir::{
            ArrayLeaf, BoundCardinality, BoundInteger, BoundNumber, BoundRational, CanonicalJson,
            Divisors, ExcludedDivisors, IntegerLeaf, NumberLeaf, ObjectLeaf, ObjectViolation,
            SchemaKind, StringLeaf,
        },
        CanonicalSchema,
    },
    JsonType, JsonTypeSet,
};

pub use crate::canonical::ir::{CanonicalKind, Distinctness};

impl CanonicalKind {
    /// Stable `snake_case` label of this kind (e.g. `"multi_type"`, `"raw"`).
    #[must_use]
    pub fn as_str(self) -> &'static str {
        self.into()
    }
}

impl Distinctness {
    /// Stable `snake_case` label of this state (e.g. `"all_distinct"`).
    #[must_use]
    pub fn as_str(self) -> &'static str {
        self.into()
    }
}

/// A canonical node: one arm per IR variant.
///
/// Exhaustive on purpose: a new variant must break every consumer that maps views, the bindings
/// included, rather than reaching a runtime fallback.
#[derive(Debug, Clone, PartialEq)]
pub enum CanonicalView {
    /// A value matches iff its JSON type is in the set.
    MultiType(JsonTypeSet),
    /// A value matches iff its JSON type is `ty` *and* it satisfies `body`; other types do not match.
    TypedGroup(TypedGroupView),
    /// A string value within a length window.
    String(StringView),
    /// An integer value within a range.
    Integer(IntegerView),
    /// A number value within a real interval.
    Number(NumberView),
    /// An array value whose length is within a window.
    Array(ArrayView),
    /// An object value within its constraints.
    Object(ObjectView),
    Const(Value),
    Enum(Vec<Value>),
    /// The exact complement of an opaque schema, keeping the references it names symbolic.
    Not(Box<CanonicalSchema>),
    /// A value matches iff every opaque branch matches.
    AllOf(Vec<CanonicalSchema>),
    /// A value matches iff at least one branch matches.
    AnyOf(Vec<CanonicalSchema>),
    /// A value matches iff exactly one branch matches.
    OneOf(Vec<CanonicalSchema>),
    Reference(String),
    True,
    False,
    Raw(Value),
}

/// Payload of [`CanonicalView::TypedGroup`]: JSON type `ty` and a `body` schema constraining its values.
#[derive(Debug, Clone, PartialEq)]
pub struct TypedGroupView {
    pub ty: JsonType,
    pub body: CanonicalSchema,
}

/// Payload of [`CanonicalView::String`]: the `minLength`/`maxLength` bounds and the required and
/// barred facets on a string value.
#[derive(Debug, Clone, PartialEq)]
pub struct StringView {
    pub min_length: Option<Number>,
    pub max_length: Option<Number>,
    pub patterns: Vec<String>,
    /// Patterns the string must not match.
    pub excluded_patterns: Vec<String>,
    pub formats: Vec<String>,
    /// Formats the string must fail.
    pub excluded_formats: Vec<String>,
    pub content_media_types: Vec<String>,
    pub content_encodings: Vec<String>,
    /// Values the string must differ from.
    pub excluded: Vec<String>,
}

/// Payload of [`CanonicalView::Number`]: the interval bounds on a number value, each with whether
/// the bound itself is admitted, and the divisor every admitted value is a multiple of.
#[derive(Debug, Clone, PartialEq)]
pub struct NumberView {
    pub minimum: Option<Number>,
    pub exclusive_minimum: bool,
    pub maximum: Option<Number>,
    pub exclusive_maximum: bool,
    pub multiple_of: Vec<Number>,
    /// Divisors no admitted value is a multiple of.
    pub not_multiple_of: Vec<Number>,
    /// No admitted value is one of the draft's integers (Draft 4 only; later drafts respell this
    /// as a barred divisor of one).
    pub excludes_integers: bool,
}

/// Payload of [`CanonicalView::Array`]: the constraints on an array value.
// The fields carry the keywords they came from, whose names share the suffix.
#[allow(clippy::struct_field_names)]
#[derive(Debug, Clone, PartialEq)]
pub struct ArrayView {
    pub min_items: Option<Number>,
    pub max_items: Option<Number>,
    /// What the array says about elements coinciding.
    pub distinctness: Distinctness,
    /// Per-index schemas: the element at position `i` satisfies `prefix_items[i]`.
    pub prefix_items: Vec<CanonicalSchema>,
    /// The schema every element from `prefix_items.len()` onward satisfies.
    pub items: Option<CanonicalSchema>,
    /// Existential demands: the number of elements matching each entry's schema sits in its window.
    pub contains: Vec<ContainsView>,
}

/// One `contains` demand of an array. An absent minimum spells the default of one.
#[derive(Debug, Clone, PartialEq)]
pub struct ContainsView {
    pub schema: CanonicalSchema,
    pub min_contains: Option<Number>,
    pub max_contains: Option<Number>,
}

/// Payload of [`CanonicalView::Object`]: the constraints on an object value.
#[derive(Debug, Clone, PartialEq)]
pub struct ObjectView {
    pub min_properties: Option<Number>,
    pub max_properties: Option<Number>,
    pub required: Vec<String>,
    /// The schema every key satisfies, narrowed to the string domain.
    pub property_names: Option<CanonicalSchema>,
    /// The schema each named key satisfies when the object carries it.
    pub properties: BTreeMap<String, CanonicalSchema>,
    /// The schema every key matching the pattern satisfies when the object carries it.
    pub pattern_properties: BTreeMap<String, CanonicalSchema>,
    /// The schema every key `properties` does not name satisfies.
    pub additional_properties: Option<CanonicalSchema>,
    /// The object must break each of these rules: negation stores the rule itself, so emitting
    /// wraps it in `not` verbatim.
    pub violations: Vec<ObjectViolationView>,
}

/// One demand produced by negation: the object must hold at least one entry that breaks the
/// stored rule.
#[derive(Debug, Clone, PartialEq)]
pub enum ObjectViolationView {
    /// Some key's name fails the schema.
    NameFails(CanonicalSchema),
    /// Some key outside `names` and matching none of `patterns` has a value failing `additional`.
    UndeclaredValueFails {
        names: Vec<String>,
        patterns: Vec<String>,
        additional: CanonicalSchema,
    },
}

/// Payload of [`CanonicalView::Integer`]: the interval bounds and divisor on an integer value.
#[derive(Debug, Clone, PartialEq)]
pub struct IntegerView {
    pub minimum: Option<Number>,
    pub maximum: Option<Number>,
    pub multiple_of: Vec<Number>,
    /// Divisors no admitted value is a multiple of.
    pub not_multiple_of: Vec<Number>,
}

impl CanonicalSchema {
    /// This node's structural view.
    #[must_use]
    pub fn view(&self) -> CanonicalView {
        match self.schema_kind() {
            SchemaKind::MultiType(set) => CanonicalView::MultiType(*set),
            SchemaKind::TypedGroup { ty, body } => CanonicalView::TypedGroup(TypedGroupView {
                ty: *ty,
                body: self.wrap_child(body),
            }),
            SchemaKind::String(leaf) => CanonicalView::String(string_view(leaf.get())),
            SchemaKind::Integer(bounds) => CanonicalView::Integer(integer_view(bounds.get())),
            SchemaKind::Number(leaf) => CanonicalView::Number(number_view(leaf.get())),
            SchemaKind::Array(leaf) => CanonicalView::Array(array_view(
                leaf.get(),
                leaf.get()
                    .prefix
                    .iter()
                    .map(|schema| self.wrap_child(schema))
                    .collect(),
                leaf.get()
                    .items
                    .as_ref()
                    .map(|items| self.wrap_child(items)),
                leaf.get()
                    .contains
                    .iter()
                    .map(|facet| ContainsView {
                        schema: self.wrap_child(&facet.schema),
                        min_contains: facet.minimum.as_ref().map(BoundCardinality::to_number),
                        max_contains: facet.maximum.as_ref().map(BoundCardinality::to_number),
                    })
                    .collect(),
            )),
            SchemaKind::Object(leaf) => CanonicalView::Object(object_view(
                leaf.get(),
                leaf.get()
                    .property_names
                    .as_ref()
                    .map(|names| self.wrap_child(names)),
                leaf.get()
                    .properties
                    .iter()
                    .map(|(key, schema)| (key.to_string(), self.wrap_child(schema)))
                    .collect(),
                leaf.get()
                    .pattern_properties
                    .iter()
                    .map(|(pattern, schema)| (pattern.to_string(), self.wrap_child(schema)))
                    .collect(),
                leaf.get()
                    .additional
                    .as_ref()
                    .map(|shield| self.wrap_child(shield)),
                leaf.get()
                    .violations
                    .iter()
                    .map(|violation| match violation {
                        ObjectViolation::NameFails(violated) => {
                            ObjectViolationView::NameFails(self.wrap_child(violated))
                        }
                        ObjectViolation::UndeclaredValueFails {
                            names,
                            patterns,
                            additional,
                        } => ObjectViolationView::UndeclaredValueFails {
                            names: names.iter().map(ToString::to_string).collect(),
                            patterns: patterns.iter().map(ToString::to_string).collect(),
                            additional: self.wrap_child(additional),
                        },
                    })
                    .collect(),
            )),
            SchemaKind::Const(value) => CanonicalView::Const(value.to_value()),
            SchemaKind::Enum(values) => CanonicalView::Enum(
                values
                    .as_slice()
                    .iter()
                    .map(CanonicalJson::to_value)
                    .collect(),
            ),
            SchemaKind::Not(schema) => CanonicalView::Not(Box::new(self.wrap_child(schema))),
            SchemaKind::AllOf(branches) => CanonicalView::AllOf(
                branches
                    .as_slice()
                    .iter()
                    .map(|branch| self.wrap_child(branch))
                    .collect(),
            ),
            SchemaKind::AnyOf(branches) => CanonicalView::AnyOf(
                branches
                    .as_slice()
                    .iter()
                    .map(|branch| self.wrap_child(branch))
                    .collect(),
            ),
            SchemaKind::OneOf(branches) => CanonicalView::OneOf(
                branches
                    .iter()
                    .map(|branch| self.wrap_child(branch))
                    .collect(),
            ),
            SchemaKind::Reference(uri) => CanonicalView::Reference(uri.to_string()),
            SchemaKind::True => CanonicalView::True,
            SchemaKind::False => CanonicalView::False,
            SchemaKind::Raw(_) => CanonicalView::Raw(self.to_json_schema()),
        }
    }

    /// This node's structural kind.
    #[must_use]
    pub fn kind(&self) -> CanonicalKind {
        self.schema_kind().into()
    }
}

fn number_view(leaf: &NumberLeaf) -> NumberView {
    NumberView {
        minimum: leaf.minimum.as_ref().map(BoundNumber::to_number),
        exclusive_minimum: leaf.minimum.as_ref().is_some_and(|b| !b.is_inclusive()),
        maximum: leaf.maximum.as_ref().map(BoundNumber::to_number),
        exclusive_maximum: leaf.maximum.as_ref().is_some_and(|b| !b.is_inclusive()),
        multiple_of: divisor_numbers(&leaf.multiple_of),
        not_multiple_of: excluded_divisor_numbers(&leaf.not_multiple_of),
        excludes_integers: leaf.excludes_integers,
    }
}

fn integer_view(leaf: &IntegerLeaf) -> IntegerView {
    IntegerView {
        minimum: leaf.bounds.minimum.as_ref().map(BoundInteger::to_number),
        maximum: leaf.bounds.maximum.as_ref().map(BoundInteger::to_number),
        multiple_of: divisor_numbers(&leaf.multiple_of),
        not_multiple_of: excluded_divisor_numbers(&leaf.not_multiple_of),
    }
}

fn excluded_divisor_numbers(barred: &ExcludedDivisors) -> Vec<Number> {
    barred
        .as_slice()
        .iter()
        .map(BoundRational::to_number)
        .collect()
}

// The element-schema children need the schema-level wrapping only the caller can do, so they arrive
// already wrapped instead of being read off the leaf.
fn array_view(
    leaf: &ArrayLeaf,
    prefix_items: Vec<CanonicalSchema>,
    items: Option<CanonicalSchema>,
    contains: Vec<ContainsView>,
) -> ArrayView {
    ArrayView {
        prefix_items,
        items,
        contains,
        min_items: leaf
            .lengths
            .minimum
            .as_ref()
            .map(BoundCardinality::to_number),
        max_items: leaf
            .lengths
            .maximum
            .as_ref()
            .map(BoundCardinality::to_number),
        distinctness: leaf.distinctness,
    }
}

// The nested children need the schema-level wrapping only the caller can do, so they arrive already
// wrapped instead of being read off the leaf.
fn object_view(
    leaf: &ObjectLeaf,
    property_names: Option<CanonicalSchema>,
    properties: BTreeMap<String, CanonicalSchema>,
    pattern_properties: BTreeMap<String, CanonicalSchema>,
    additional_properties: Option<CanonicalSchema>,
    violations: Vec<ObjectViolationView>,
) -> ObjectView {
    ObjectView {
        min_properties: leaf.sizes.minimum.as_ref().map(BoundCardinality::to_number),
        max_properties: leaf.sizes.maximum.as_ref().map(BoundCardinality::to_number),
        required: leaf.required.iter().map(ToString::to_string).collect(),
        property_names,
        properties,
        pattern_properties,
        additional_properties,
        violations,
    }
}

fn string_view(leaf: &StringLeaf) -> StringView {
    StringView {
        min_length: leaf
            .lengths
            .minimum
            .as_ref()
            .map(BoundCardinality::to_number),
        max_length: leaf
            .lengths
            .maximum
            .as_ref()
            .map(BoundCardinality::to_number),
        patterns: leaf.patterns.iter().map(ToString::to_string).collect(),
        excluded_patterns: leaf
            .excluded_patterns
            .iter()
            .map(ToString::to_string)
            .collect(),
        formats: leaf.formats.iter().map(ToString::to_string).collect(),
        excluded_formats: leaf
            .excluded_formats
            .iter()
            .map(ToString::to_string)
            .collect(),
        content_media_types: leaf
            .content_media_types
            .iter()
            .map(ToString::to_string)
            .collect(),
        content_encodings: leaf
            .content_encodings
            .iter()
            .map(ToString::to_string)
            .collect(),
        excluded: leaf.excluded.iter().map(ToString::to_string).collect(),
    }
}

fn divisor_numbers(divisors: &Divisors) -> Vec<Number> {
    divisors
        .as_slice()
        .iter()
        .map(BoundRational::to_number)
        .collect()
}