jsonschema 0.49.2

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
use crate::canonical::ir::{
    ArrayLeaf, BoundCardinality, Bounds, ContainsFacet, LengthBounds, Schema,
};

/// Array leaves merged per uniqueness flag and item schema, free of subsumed windows. Inserts are
/// batched; the form is restored before any read, so the order in which leaves arrive cannot
/// change the result.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ArrayLeaves {
    leaves: Vec<ArrayLeaf>,
    canonical: bool,
}

impl Default for ArrayLeaves {
    fn default() -> Self {
        Self {
            leaves: Vec::new(),
            canonical: true,
        }
    }
}

impl ArrayLeaves {
    pub(crate) fn insert(&mut self, leaf: ArrayLeaf) {
        self.leaves.push(leaf);
        self.canonical = false;
    }

    fn canonicalize(&mut self) {
        if self.canonical {
            return;
        }
        let was_empty = self.leaves.is_empty();
        self.leaves = merge(std::mem::take(&mut self.leaves));
        extend_over_bare_windows(&mut self.leaves);
        hand_off_empty(&mut self.leaves);
        // Extending can overlap the windows of leaves sharing their facets; fold those again.
        self.leaves = merge(std::mem::take(&mut self.leaves));
        absorb_trivially_distinct(&mut self.leaves);
        absorb_trivially_conforming(&mut self.leaves);
        drop_subsumed(&mut self.leaves);
        self.canonical = true;
        // `is_empty` reads the batch without canonicalizing, which relies on this.
        debug_assert_eq!(
            self.leaves.is_empty(),
            was_empty,
            "merging emptied the leaves"
        );
    }

    pub(crate) fn clear(&mut self) {
        self.leaves.clear();
        self.canonical = true;
    }

    /// Dropping leaves can neither make two of the rest mergeable nor subsume one by another.
    pub(crate) fn retain(&mut self, keep: impl FnMut(&ArrayLeaf) -> bool) {
        self.canonicalize();
        self.leaves.retain(keep);
    }

    /// Merging never removes the last leaf, so this reads the batch without canonicalizing.
    pub(crate) fn is_empty(&self) -> bool {
        self.leaves.is_empty()
    }

    pub(crate) fn as_slice(&mut self) -> &[ArrayLeaf] {
        self.canonicalize();
        &self.leaves
    }
}

impl IntoIterator for ArrayLeaves {
    type Item = ArrayLeaf;
    type IntoIter = std::vec::IntoIter<ArrayLeaf>;

    fn into_iter(mut self) -> Self::IntoIter {
        self.canonicalize();
        self.leaves.into_iter()
    }
}

/// Fold the length windows of leaves that agree on uniqueness and the item schema.
/// e.g.  anyOf [
///         {"type": "array", "uniqueItems": true, "maxItems": 2},
///         {"type": "array", "uniqueItems": true, "minItems": 3}
///       ]  =>  {"type": "array", "uniqueItems": true}
///
/// One demanding distinct items admits fewer arrays, so those leaves stay apart.
/// e.g.  anyOf [
///         {"type": "array", "uniqueItems": true, "minItems": 5},
///         {"type": "array", "maxItems": 2}
///       ]  =>  unchanged
fn merge(mut leaves: Vec<ArrayLeaf>) -> Vec<ArrayLeaf> {
    if leaves.len() < 2 {
        return leaves;
    }
    leaves.sort_by(|left, right| {
        (left.unique, &left.prefix, &left.items, &left.contains).cmp(&(
            right.unique,
            &right.prefix,
            &right.items,
            &right.contains,
        ))
    });
    let mut merged: Vec<ArrayLeaf> = Vec::with_capacity(leaves.len());
    let mut windows: Vec<LengthBounds> = Vec::new();
    let mut facets: Option<Facets> = None;
    for leaf in leaves {
        if facets.as_ref().is_none_or(|group| {
            group.unique != leaf.unique
                || group.prefix != leaf.prefix
                || group.items != leaf.items
                || group.contains != leaf.contains
        }) {
            flush_group(&mut merged, facets.take(), &mut windows);
            facets = Some(Facets {
                unique: leaf.unique,
                prefix: leaf.prefix,
                items: leaf.items,
                contains: leaf.contains,
            });
        }
        windows.push(leaf.lengths);
    }
    flush_group(&mut merged, facets, &mut windows);
    merged
}

/// The facets shared by a merge group; only the length window differs within one.
struct Facets {
    unique: bool,
    prefix: Vec<Schema>,
    items: Option<Schema>,
    contains: Vec<ContainsFacet>,
}

/// Emit one leaf per merged window, all carrying the group's facets.
fn flush_group(
    merged: &mut Vec<ArrayLeaf>,
    facets: Option<Facets>,
    windows: &mut Vec<LengthBounds>,
) {
    let Some(Facets {
        unique,
        prefix,
        items,
        contains,
    }) = facets
    else {
        return;
    };
    let mut lengths = Bounds::merge_all(std::mem::take(windows));
    let last = lengths.pop().expect("a group holds at least one window");
    for window in lengths {
        merged.push(ArrayLeaf {
            lengths: window,
            unique,
            prefix: prefix.clone(),
            items: items.clone(),
            contains: contains.clone(),
        });
    }
    merged.push(ArrayLeaf {
        lengths: last,
        unique,
        prefix,
        items,
        contains,
    });
}

/// Widen a facet-carrying window over a bare sibling window it touches: the lengths gained lie
/// inside the bare window, which admits those arrays with any content, so the union is unchanged.
/// The boundary between the two then has one spelling, whatever the facet leaf's window said.
/// ```text
/// e.g.  anyOf [
///         {"type": "array", "maxItems": 1},
///         {"type": "array", "items": {"type": "integer"}, "minItems": 2}
///       ]  =>  anyOf [
///         {"type": "array", "maxItems": 1},
///         {"type": "array", "items": {"type": "integer"}}
///       ]
/// ```
fn extend_over_bare_windows(leaves: &mut [ArrayLeaf]) {
    let bare: Vec<LengthBounds> = leaves
        .iter()
        .filter(|leaf| {
            !leaf.unique
                && leaf.prefix.is_empty()
                && leaf.items.is_none()
                && leaf.contains.is_empty()
        })
        .map(|leaf| leaf.lengths.clone())
        .collect();
    if bare.is_empty() {
        return;
    }
    for leaf in leaves.iter_mut() {
        if !leaf.unique
            && leaf.prefix.is_empty()
            && leaf.items.is_none()
            && leaf.contains.is_empty()
        {
            continue;
        }
        // A grown window can reach the next bare window, so retry until none applies.
        loop {
            let mut grown = false;
            for window in &bare {
                let merged = Bounds::merge_all(vec![leaf.lengths.clone(), window.clone()]);
                if let Ok([merged]) = <[_; 1]>::try_from(merged) {
                    if merged != leaf.lengths {
                        leaf.lengths = merged;
                        grown = true;
                    }
                }
            }
            if !grown {
                break;
            }
        }
    }
}

/// Drop a `minItems: 1` when another branch admits the empty array: the drop adds only `[]`, which
/// that branch accepts and which satisfies any item schema and distinctness vacuously.
/// ```text
/// e.g.  anyOf [
///         {"type": "array", "uniqueItems": true, "minItems": 1},
///         {"type": "array", "items": {"type": "integer"}}
///       ]  =>  anyOf [
///         {"type": "array", "uniqueItems": true},
///         {"type": "array", "items": {"type": "integer"}}
///       ]
/// ```
fn hand_off_empty(leaves: &mut [ArrayLeaf]) {
    // A `contains` demand floors the length on its own, so such a leaf rejects the empty array
    // even with no window minimum.
    if !leaves.iter().any(|leaf| {
        leaf.lengths
            .minimum
            .as_ref()
            .is_none_or(BoundCardinality::is_zero)
            && leaf
                .contains
                .iter()
                .all(|facet| facet.effective_minimum().is_zero())
    }) {
        return;
    }
    let one = BoundCardinality::from(1);
    for leaf in leaves.iter_mut() {
        if leaf.lengths.minimum.as_ref() == Some(&one) {
            leaf.lengths.minimum = None;
        }
    }
}

/// A window of at most one item holds nothing that can repeat, so its arrays are distinct already:
/// widen a neighbouring leaf that demands distinctness over it.
/// e.g.  anyOf [
///         {"type": "array", "maxItems": 1},
///         {"type": "array", "uniqueItems": true, "minItems": 2}
///       ]  =>  {"type": "array", "uniqueItems": true}
///
/// A gap between the two leaves keeps them apart, since the lengths between them admit repeats.
/// e.g.  anyOf [
///         {"type": "array", "maxItems": 1},
///         {"type": "array", "uniqueItems": true, "minItems": 4}
///       ]  =>  unchanged
fn absorb_trivially_distinct(leaves: &mut Vec<ArrayLeaf>) {
    let Some(trivial) = leaves.iter().position(|leaf| {
        !leaf.unique
            && leaf.prefix.is_empty()
            && leaf.items.is_none()
            && leaf.contains.is_empty()
            && leaf
                .lengths
                .maximum
                .as_ref()
                .is_some_and(|max| *max <= BoundCardinality::from(1))
    }) else {
        return;
    };
    let window = leaves[trivial].lengths.clone();
    // Merging the pair yields one window exactly when they overlap or touch. An element-constrained
    // leaf cannot widen over arrays of one item, whose element it never checked; a `contains` leaf
    // cannot widen over the empty array, which its demand rejects.
    let Some((target, widened)) = leaves.iter().enumerate().find_map(|(index, leaf)| {
        if !leaf.unique
            || leaf.items.is_some()
            || !leaf.prefix.is_empty()
            || !leaf.contains.is_empty()
        {
            return None;
        }
        let mut merged = Bounds::merge_all(vec![leaf.lengths.clone(), window.clone()]);
        (merged.len() == 1).then(|| (index, merged.pop().expect("a merged window")))
    }) else {
        return;
    };
    leaves[target].lengths = widened;
    leaves.remove(trivial);
}

/// A window of no items holds only the empty array, whose elements satisfy any item schema
/// vacuously: widen a neighbouring item-constrained leaf over it.
/// ```text
/// e.g.  anyOf [
///         {"const": []},
///         {"type": "array", "items": {"type": "integer"}, "minItems": 1}
///       ]  =>  {"type": "array", "items": {"type": "integer"}}
/// ```
///
/// A gap between the two windows keeps them apart: widening would admit the lengths between them.
/// ```text
/// e.g.  anyOf [
///         {"const": []},
///         {"type": "array", "items": {"type": "integer"}, "minItems": 2}
///       ]  =>  unchanged
/// ```
fn absorb_trivially_conforming(leaves: &mut Vec<ArrayLeaf>) {
    let Some(trivial) = leaves.iter().position(|leaf| {
        !leaf.unique
            && leaf.prefix.is_empty()
            && leaf.items.is_none()
            && leaf.contains.is_empty()
            && leaf
                .lengths
                .maximum
                .as_ref()
                .is_some_and(BoundCardinality::is_zero)
    }) else {
        return;
    };
    let window = leaves[trivial].lengths.clone();
    // Merging the pair yields one window exactly when they overlap or touch. A `contains` leaf
    // stays apart: the empty array fails its demand, so widening over it would admit too much.
    let Some((target, widened)) = leaves
        .iter()
        .enumerate()
        .filter(|(_, leaf)| {
            (leaf.items.is_some() || !leaf.prefix.is_empty()) && leaf.contains.is_empty()
        })
        .find_map(|(index, leaf)| {
            let merged = Bounds::merge_all(vec![leaf.lengths.clone(), window.clone()]);
            match <[_; 1]>::try_from(merged) {
                Ok([widened]) => Some((index, widened)),
                Err(_) => None,
            }
        })
    else {
        return;
    };
    leaves[target].lengths = widened;
    leaves.remove(trivial);
}

/// Drop a leaf whose arrays another leaf already admits: a window that contains it, not demanding
/// distinctness unless it does too.
/// e.g.  anyOf [
///         {"type": "array"},
///         {"type": "array", "uniqueItems": true}
///       ]  =>  {"type": "array"}
fn drop_subsumed(leaves: &mut Vec<ArrayLeaf>) {
    if leaves.len() < 2 {
        return;
    }
    let mut keep = vec![true; leaves.len()];
    for (index, leaf) in leaves.iter().enumerate() {
        for (other_index, other) in leaves.iter().enumerate() {
            if index == other_index || !keep[other_index] || !keep[index] {
                continue;
            }
            // Element constraints are compared only for equality: deciding that one schema admits
            // every element another does needs a subsumption test the algebra does not have. So
            // `other` is looser exactly when its per-index schemas are an equal prefix of `leaf`'s
            // and it places nothing beyond them, while `leaf` constrains further.
            let looser_items = other.items.is_none()
                && other.contains.is_empty()
                && leaf.prefix.starts_with(&other.prefix)
                && (leaf.items.is_some()
                    || leaf.prefix.len() > other.prefix.len()
                    || !leaf.contains.is_empty());
            let same_elements = other.prefix == leaf.prefix
                && other.items == leaf.items
                && other.contains == leaf.contains;
            let wider = other.lengths.covers(&leaf.lengths)
                && (!other.unique || leaf.unique)
                && (looser_items || same_elements);
            // Leaves agreeing on every facet but the window were folded by merging, so one of the
            // facets is strictly looser here and decides which leaf goes.
            debug_assert!(
                !wider || other.unique != leaf.unique || !same_elements,
                "merging left two leaves carrying the same facets"
            );
            if wider && ((leaf.unique && !other.unique) || looser_items) {
                keep[index] = false;
            }
        }
    }
    let mut index = 0;
    leaves.retain(|_| {
        let keeps = keep[index];
        index += 1;
        keeps
    });
}