jsonschema 0.58.5

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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//! Configuration and entry points for canonicalization.

use std::{
    collections::{BTreeMap, BTreeSet},
    sync::{Arc, Mutex},
};

use ahash::{AHashMap, AHashSet};
use referencing::{Draft, Registry, Retrieve, Uri};
use serde_json::Value;

use crate::{
    canonical::{
        context::{CanonicalizationContext, SharedRegexes},
        emptiness,
        ir::{RawJson, RawReason, Schema, SchemaKind},
        parse::{self, Seed},
        refold,
        schema::CanonicalSchema,
        CanonicalizationError, DefinitionMap, ROOT_DEFINITION_KEY,
    },
    compiler::{
        formats_are_assertions_by_default, normalize_base_uri, resolve_base_uri, validate_schema,
    },
    options::{PatternEngineOptions, PatternOptions},
};

/// Build a [`CanonicalizeOptions`] for configurable canonicalization.
#[must_use]
pub fn options() -> CanonicalizeOptions<'static> {
    CanonicalizeOptions::default()
}

/// Why a subschema admits no value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnsatisfiableReason {
    /// Written as `false`.
    Literal,
    /// One part every value must satisfy admits nothing by itself. A subschema part has a
    /// reason of its own under its pointer.
    Empty(Cause),
    /// Each part admits values; no value satisfies all of them together.
    Conflict(Vec<Cause>),
}

/// A part of a schema object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cause {
    /// JSON Pointer of the schema object holding `keywords`, or of the subschema itself.
    pub pointer: String,
    /// Keywords of one family present at `pointer`; empty for a whole subschema.
    pub keywords: Vec<String>,
}

/// Configurable canonicalization entry point. Construct via [`options`].
#[derive(Default)]
pub struct CanonicalizeOptions<'r> {
    registry: Option<&'r Registry<'r>>,
    retriever: Option<Arc<dyn Retrieve>>,
    base_uri: Option<String>,
    pattern_options: PatternEngineOptions,
    draft: Option<Draft>,
    validate_formats: Option<bool>,
}

impl<'r> CanonicalizeOptions<'r> {
    /// Use a pre-built [`Registry`] for dialect and `$ref` resolution.
    #[must_use]
    pub fn with_registry(mut self, registry: &'r Registry<'r>) -> Self {
        self.registry = Some(registry);
        self
    }

    /// Fetch external resources that are not present in the registry.
    #[must_use]
    pub fn with_retriever(mut self, retriever: impl Retrieve + 'static) -> Self {
        self.retriever = Some(Arc::new(retriever));
        self
    }

    /// Refuse to fetch any reference that is not already in the registry.
    #[must_use]
    pub fn offline(mut self) -> Self {
        self.retriever = Some(Arc::new(crate::retriever::OfflineRetriever));
        self
    }

    /// Use this URI as the base for resolving relative references in the root schema.
    ///
    /// Takes precedence over the root `$id`.
    #[must_use]
    pub fn with_base_uri(mut self, base_uri: impl Into<String>) -> Self {
        self.base_uri = Some(base_uri.into());
        self
    }

    /// Use this draft for canonicalization, overriding `$schema` detection.
    #[must_use]
    pub fn with_draft(mut self, draft: Draft) -> Self {
        self.draft = Some(draft);
        self
    }

    /// Set whether canonicalization treats `format` as a validation assertion.
    ///
    /// Left unset, it follows the draft default (Draft 4/6/7 assert known formats; 2019-09/2020-12 annotate).
    /// Asserting lets incompatible format intersections like `date`/`uuid` collapse to `false`.
    #[must_use]
    pub fn should_validate_formats(mut self, enabled: bool) -> Self {
        self.validate_formats = Some(enabled);
        self
    }

    /// Select the regular-expression engine used for `pattern` compilation and membership.
    #[must_use]
    #[allow(clippy::needless_pass_by_value)]
    pub fn with_pattern_options<E>(mut self, options: PatternOptions<E>) -> Self {
        self.pattern_options = options.inner;
        self
    }

    /// Run canonicalization with the configured options.
    ///
    /// # Errors
    ///
    /// Same as [`crate::canonicalize`].
    pub fn canonicalize(self, value: &Value) -> Result<CanonicalSchema, CanonicalizationError> {
        self.prepare(value)?.canonicalize()
    }

    /// Prepare `value` for canonicalizing its subschemas.
    ///
    /// The document decides the draft, the base URI and what `#` means, so a subschema selected
    /// from a prepared document resolves its references as it does in place. Resolving the draft,
    /// validating the document and indexing it depends only on the document, so preparing once
    /// pays for it once however many subschemas are then selected.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonschema::canonical::options;
    /// use serde_json::json;
    ///
    /// let document = json!({
    ///     "$defs": {
    ///         "Named": {"type": "object", "required": ["name"]},
    ///         "Pet": {"allOf": [
    ///             {"$ref": "#/$defs/Named"},
    ///             {"properties": {"age": {"type": "integer", "minimum": 0}}}
    ///         ]}
    ///     }
    /// });
    ///
    /// let prepared = options().prepare(&document)?;
    /// assert_eq!(
    ///     prepared.canonicalize_at("/$defs/Pet")?.to_json_schema(),
    ///     json!({
    ///         "$schema": "https://json-schema.org/draft/2020-12/schema",
    ///         "type": "object",
    ///         "properties": {"age": {"type": "integer", "minimum": 0}},
    ///         "required": ["name"]
    ///     })
    /// );
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Same as [`canonicalize`](Self::canonicalize), for the document itself.
    pub fn prepare<'a>(
        self,
        value: &'a Value,
    ) -> Result<PreparedDocument<'a>, CanonicalizationError>
    where
        'r: 'a,
    {
        prepare(value, &self)
    }
}

/// A document indexed once, ready to canonicalize any number of its subschemas.
///
/// Built by [`CanonicalizeOptions::prepare`].
pub struct PreparedDocument<'a> {
    document: &'a Value,
    draft: Draft,
    pattern_options: PatternEngineOptions,
    validate_formats: bool,
    // `None` when the draft is unknown: nothing resolves, and every selection stays verbatim.
    resolution: Option<(Registry<'a>, Uri<String>)>,
    regexes: SharedRegexes,
    /// The definition bodies the reads of this document have parsed so far, which its later reads
    /// reuse. Grown out of those reads, so a document read once pays nothing for it.
    seed: Mutex<Option<Arc<Seed>>>,
}

impl PreparedDocument<'_> {
    /// The draft the document was read under.
    #[must_use]
    pub fn draft(&self) -> Draft {
        self.draft
    }

    /// Canonicalize the document itself.
    ///
    /// # Errors
    ///
    /// Same as [`crate::canonicalize`].
    pub fn canonicalize(&self) -> Result<CanonicalSchema, CanonicalizationError> {
        self.reduce(self.document)
    }

    /// Canonicalize the subschema at `pointer`, in the document's context.
    ///
    /// # Errors
    ///
    /// Same as [`crate::canonicalize`], plus [`CanonicalizationError::PointerNotFound`] when
    /// `pointer` names nothing.
    pub fn canonicalize_at(&self, pointer: &str) -> Result<CanonicalSchema, CanonicalizationError> {
        let target = referencing::pointer(self.document, pointer)
            .ok_or_else(|| CanonicalizationError::PointerNotFound(pointer.to_string()))?;
        match target {
            Value::Bool(_) | Value::Object(_) => self.reduce(target),
            other @ (Value::Null | Value::Number(_) | Value::String(_) | Value::Array(_)) => {
                Err(CanonicalizationError::InvalidSchemaType(other.to_string()))
            }
        }
    }

    /// The subschemas that admit no value, by pointer, each with why.
    ///
    /// A pointer left out is not proven satisfiable: an unmodeled document reports nothing, like
    /// [`Satisfiability::Unknown`](crate::canonical::Satisfiability).
    ///
    /// # Errors
    ///
    /// Same as [`crate::canonicalize`], for the document itself.
    pub fn unsatisfiable(
        &self,
    ) -> Result<BTreeMap<String, UnsatisfiableReason>, CanonicalizationError> {
        let Some((registry, base_uri)) = &self.resolution else {
            return Ok(BTreeMap::new());
        };
        let resolver = registry.resolver(base_uri.clone());
        let context =
            CanonicalizationContext::new(self.draft, self.pattern_options, self.validate_formats)
                .sharing_regexes(Arc::clone(&self.regexes));
        let Some(mut parsed) = parse::parse_tracking_nodes(self.document, &context, &resolver)?
        else {
            return Ok(BTreeMap::new());
        };
        // A node holding a reference may still fold to nothing once every body is known, as
        // `canonicalize` folds it; it is settled and named the same way here.
        let mut settled_reasons = AHashMap::default();
        if !parsed.unsettled.is_empty() {
            if let Some(settled) = refold::Settled::of(&parsed, &context) {
                for (key, body) in settled.definitions() {
                    if matches!(body.kind(), SchemaKind::False) {
                        parsed
                            .parsed_definitions
                            .insert(Arc::clone(key), parse::ParsedBody::Unsatisfiable);
                    }
                }
                let unsettled: AHashMap<usize, &Schema> = parsed
                    .unsettled
                    .iter()
                    .map(|(address, schema)| (*address, schema))
                    .collect();
                settle_nodes(
                    self.document,
                    &unsettled,
                    &settled,
                    &parsed.kept(),
                    &mut settled_reasons,
                );
            }
        }
        let mut named = AHashSet::default();
        let reasons = parsed
            .parsed_nodes
            .values()
            .filter_map(|node| match node {
                parse::ParsedNode::Unsatisfiable(reason) => Some(reason),
                parse::ParsedNode::Reference(_) => None,
            })
            .chain(settled_reasons.values());
        for reason in reasons {
            reason.for_each_node(&mut |address| {
                named.insert(address);
            });
        }
        let mut empties = Vec::new();
        let mut located = AHashMap::default();
        locate_unsatisfiable(
            self.document,
            &mut String::new(),
            &parsed,
            &settled_reasons,
            &named,
            &mut empties,
            &mut located,
        );
        Ok(empties
            .into_iter()
            .map(|(pointer, value, reason)| {
                let reason = resolve_reason(reason, &pointer, value, &located);
                (pointer, reason)
            })
            .collect())
    }

    /// The definition bodies earlier reads of this document parsed. A poisoned cache reads as
    /// empty: a body is only ever a parse this document would repeat.
    fn seed(&self) -> Option<Arc<Seed>> {
        self.seed.lock().ok()?.clone()
    }

    /// Keep the bodies a read reached, so however many subschemas are selected next, each body is
    /// parsed once for the document rather than once per read.
    fn grow_seed(&self, grown: Seed) {
        if let Ok(mut seed) = self.seed.lock() {
            *seed = Some(Arc::new(grown));
        }
    }

    fn reduce(&self, target: &Value) -> Result<CanonicalSchema, CanonicalizationError> {
        let opaque = |target: &Value, reason: RawReason, pointer: Option<Arc<str>>| {
            CanonicalSchema::new(
                Schema::new(SchemaKind::Raw(RawJson::new(
                    target.clone(),
                    reason,
                    pointer,
                ))),
                self.draft,
                self.pattern_options,
                self.validate_formats,
                Arc::new(DefinitionMap::new()),
                Arc::new(BTreeSet::new()),
            )
        };
        let Some((registry, base_uri)) = &self.resolution else {
            return Ok(opaque(target, RawReason::UnknownDialect, None));
        };
        let resolver = registry.resolver(base_uri.clone());
        let context =
            CanonicalizationContext::new(self.draft, self.pattern_options, self.validate_formats)
                .sharing_regexes(Arc::clone(&self.regexes));
        let seed = self.seed();
        let (parsed, grown) = parse::parse(target, &context, &resolver, seed.as_deref())?;
        if let Some(grown) = grown {
            self.grow_seed(grown);
        }
        let Some(parsed) = parsed else {
            let reason = raw_reason(&context);
            // Only an unmodeled construct sits at one node; a run out of allowance gave up on the
            // document as a whole.
            let pointer = (reason == RawReason::Unmodeled)
                .then(|| context.declined_at())
                .flatten()
                .and_then(|address| pointer_to(target, &mut String::new(), address));
            return Ok(opaque(target, reason, pointer));
        };
        let parsed = emptiness::fold_definitions(parsed, target, &context, &resolver)?;
        // Folded now every body is known, so this entry point and the set operations agree.
        let parsed = refold::through_targets(parsed, &context);
        let (inner, definitions, local) = (
            parsed.root,
            Arc::new(parsed.definitions),
            Arc::new(parsed.local_definitions),
        );
        Ok(CanonicalSchema::new(
            inner,
            self.draft,
            self.pattern_options,
            self.validate_formats,
            definitions,
            local,
        ))
    }
}

/// Validate the document and index it for reference resolution.
fn prepare<'a, 'r: 'a>(
    value: &'a Value,
    options: &CanonicalizeOptions<'r>,
) -> Result<PreparedDocument<'a>, CanonicalizationError> {
    // Only a boolean or object is a schema document.
    match value {
        Value::Bool(_) | Value::Object(_) => {}
        other @ (Value::Null | Value::Number(_) | Value::String(_) | Value::Array(_)) => {
            return Err(CanonicalizationError::InvalidSchemaType(other.to_string()))
        }
    }
    let pattern_options = options.pattern_options;
    let draft = detect_draft(value, options.draft, options.registry)?;
    if draft == Draft::Unknown {
        return Ok(PreparedDocument {
            document: value,
            draft,
            pattern_options,
            validate_formats: options.validate_formats.unwrap_or(false),
            resolution: None,
            regexes: SharedRegexes::default(),
            seed: Mutex::new(None),
        });
    }
    let validate_formats = options
        .validate_formats
        .unwrap_or_else(|| formats_are_assertions_by_default(draft));
    validate_schema(draft, value)?;
    let resource = draft.create_resource_ref(value);
    let base_uri = resolve_base_uri(options.base_uri.as_ref(), resource.id())?;
    let mut builder = match options.registry {
        Some(registry) => registry.add(base_uri.as_str(), resource)?,
        None => Registry::new().add(base_uri.as_str(), resource)?,
    };
    if let Some(retriever) = &options.retriever {
        builder = builder.retriever(Arc::clone(retriever));
    }
    let registry = builder.draft(draft).prepare()?;
    let base_uri = normalize_base_uri(&registry, &base_uri);
    Ok(PreparedDocument {
        document: value,
        draft,
        pattern_options,
        validate_formats,
        resolution: Some((registry, base_uri)),
        regexes: SharedRegexes::default(),
        seed: Mutex::new(None),
    })
}

/// Resolve the draft: an explicit override, else detected from `$schema`.
fn detect_draft<'r>(
    value: &Value,
    draft: Option<Draft>,
    registry: Option<&'r Registry<'r>>,
) -> Result<Draft, CanonicalizationError> {
    let mut options = crate::options();
    if let Some(draft) = draft {
        options = options.with_draft(draft);
    }
    if let Some(registry) = registry {
        options = options.with_registry(registry);
    }
    options
        .draft_for(value)
        .map_err(CanonicalizationError::from)
}

/// Whether the body `key` names is unsatisfiable, reading through a chain of pointers as
/// [`CanonicalSchema::satisfiability`] does.
fn names_unsatisfiable_body(parsed: &parse::ParseOutput, key: &str) -> bool {
    let mut key = key;
    let mut walked: Vec<&str> = Vec::new();
    loop {
        if walked.contains(&key) {
            return false;
        }
        walked.push(key);
        match parsed.parsed_definitions.get(key) {
            Some(parse::ParsedBody::Unsatisfiable) => return true,
            Some(parse::ParsedBody::Reference(next)) => {
                key = next.as_ref();
                continue;
            }
            None => {}
        }
        let body = if key == ROOT_DEFINITION_KEY {
            &parsed.root
        } else {
            match parsed.definitions.get(key) {
                Some(body) => body,
                None => return false,
            }
        };
        match body.kind() {
            SchemaKind::False => return true,
            SchemaKind::Reference(next) => key = next.as_ref(),
            SchemaKind::MultiType(_)
            | SchemaKind::TypedGroup { .. }
            | SchemaKind::String(_)
            | SchemaKind::Integer(_)
            | SchemaKind::Number(_)
            | SchemaKind::Array(_)
            | SchemaKind::Object(_)
            | SchemaKind::Const(_)
            | SchemaKind::Enum(_)
            | SchemaKind::Not(_)
            | SchemaKind::AllOf(_)
            | SchemaKind::AnyOf(_)
            | SchemaKind::OneOf(_)
            | SchemaKind::True
            | SchemaKind::Raw(_) => return false,
        }
    }
}

/// Walk `value` alongside what the parse made of each node, naming the unsatisfiable ones by pointer.
/// What stopped the run, read off the context the parse left behind. An exhausted allowance also
/// records an approximation, so it is asked about first.
fn raw_reason(context: &CanonicalizationContext) -> RawReason {
    if context.outgrew_distribution() {
        RawReason::OutgrewIntersections
    } else if context.saw_inexact_intersection() {
        RawReason::InexactIntersection
    } else if context.outgrew_cases() {
        RawReason::OutgrewCases
    } else {
        RawReason::Unmodeled
    }
}

/// The pointer naming the node at `address`, or `None` where the parse declined on a schema it
/// rewrote, which the document no longer holds.
fn pointer_to(value: &Value, pointer: &mut String, address: usize) -> Option<Arc<str>> {
    if std::ptr::from_ref(value) as usize == address {
        return Some(Arc::from(pointer.as_str()));
    }
    let restore = pointer.len();
    let mut children = |children: Box<dyn Iterator<Item = (String, &Value)> + '_>| {
        for (segment, child) in children {
            pointer.push('/');
            pointer.push_str(&segment);
            if let Some(found) = pointer_to(child, pointer, address) {
                return Some(found);
            }
            pointer.truncate(restore);
        }
        None
    };
    match value {
        Value::Object(map) => children(Box::new(map.iter().map(|(key, child)| {
            let mut segment = String::new();
            referencing::write_escaped_str(&mut segment, key);
            (segment, child)
        }))),
        Value::Array(items) => children(Box::new(
            items
                .iter()
                .enumerate()
                .map(|(index, child)| (index.to_string(), child)),
        )),
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => None,
    }
}

/// An empty node as the walk found it: the recorded reason, or a pointer whose body is empty.
#[derive(Clone, Copy)]
enum Emptiness<'p> {
    Recorded(&'p parse::RecordedReason),
    ReferenceBody,
}

/// Walk `value`, settling every node the parse left holding a reference and naming why the ones
/// that fold to nothing admit no value.
fn settle_nodes(
    value: &Value,
    unsettled: &AHashMap<usize, &Schema>,
    settled: &refold::Settled,
    kept: &parse::Kept<'_>,
    out: &mut AHashMap<usize, parse::RecordedReason>,
) {
    let address = std::ptr::from_ref(value) as usize;
    if let (Some(schema), Value::Object(map)) = (unsettled.get(&address), value) {
        let folds_to_nothing = settled
            .settle(schema)
            .is_some_and(|folded| matches!(folded.kind(), SchemaKind::False));
        if folds_to_nothing {
            let sides: Vec<(parse::PartKind, Schema)> = kept
                .parts
                .get(&address)
                .expect("an unsettled node kept its sides")
                .iter()
                .map(|(kind, side)| (*kind, settled.settle(side).unwrap_or_else(|| side.clone())))
                .collect();
            out.insert(
                address,
                parse::attribute(address, map, &sides, settled.context(), kept),
            );
        }
    }
    match value {
        Value::Object(map) => {
            for child in map.values() {
                settle_nodes(child, unsettled, settled, kept, out);
            }
        }
        Value::Array(items) => {
            for child in items {
                settle_nodes(child, unsettled, settled, kept, out);
            }
        }
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
    }
}

/// Walk `value` alongside what the parse made of each node, collecting the empty nodes and the
/// pointer of every node a recorded cause names.
fn locate_unsatisfiable<'v>(
    value: &'v Value,
    pointer: &mut String,
    parsed: &'v parse::ParseOutput,
    settled: &'v AHashMap<usize, parse::RecordedReason>,
    named: &AHashSet<usize>,
    empties: &mut Vec<(String, &'v Value, Emptiness<'v>)>,
    located: &mut AHashMap<usize, (String, &'v Value)>,
) {
    let address = std::ptr::from_ref(value) as usize;
    if named.contains(&address) {
        located.insert(address, (pointer.clone(), value));
    }
    if let Some(reason) = settled.get(&address) {
        empties.push((pointer.clone(), value, Emptiness::Recorded(reason)));
    } else {
        match parsed.parsed_nodes.get(&std::ptr::from_ref(value)) {
            Some(parse::ParsedNode::Unsatisfiable(reason)) => {
                empties.push((pointer.clone(), value, Emptiness::Recorded(reason)));
            }
            Some(parse::ParsedNode::Reference(key)) if names_unsatisfiable_body(parsed, key) => {
                empties.push((pointer.clone(), value, Emptiness::ReferenceBody));
            }
            Some(parse::ParsedNode::Reference(_)) | None => {}
        }
    }
    let restore = pointer.len();
    match value {
        Value::Object(map) => {
            for (key, child) in map {
                pointer.push('/');
                referencing::write_escaped_str(pointer, key);
                locate_unsatisfiable(child, pointer, parsed, settled, named, empties, located);
                pointer.truncate(restore);
            }
        }
        Value::Array(items) => {
            let mut index_buffer = itoa::Buffer::new();
            for (index, child) in items.iter().enumerate() {
                pointer.push('/');
                pointer.push_str(index_buffer.format(index));
                locate_unsatisfiable(child, pointer, parsed, settled, named, empties, located);
                pointer.truncate(restore);
            }
        }
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
    }
}

fn resolve_reason(
    reason: Emptiness<'_>,
    pointer: &str,
    value: &Value,
    located: &AHashMap<usize, (String, &Value)>,
) -> UnsatisfiableReason {
    match reason {
        Emptiness::Recorded(parse::RecordedReason::Literal) => UnsatisfiableReason::Literal,
        Emptiness::Recorded(parse::RecordedReason::Empty(cause)) => {
            UnsatisfiableReason::Empty(resolve_cause(cause, pointer, value, located))
        }
        Emptiness::Recorded(parse::RecordedReason::Conflict(causes)) => {
            UnsatisfiableReason::Conflict(
                causes
                    .iter()
                    .map(|cause| resolve_cause(cause, pointer, value, located))
                    .collect(),
            )
        }
        Emptiness::ReferenceBody => UnsatisfiableReason::Empty(Cause {
            pointer: pointer.to_string(),
            keywords: family_keywords(parse::PartKind::Reference, value),
        }),
    }
}

/// A cause naming a node the document does not hold (a rewritten object's child) falls back to
/// what it was narrowed from, and past that to the keyword holding it at the empty node itself.
fn resolve_cause(
    cause: &parse::RecordedCause,
    enclosing: &str,
    enclosing_value: &Value,
    located: &AHashMap<usize, (String, &Value)>,
) -> Cause {
    match located.get(&cause.node) {
        Some((pointer, value)) => Cause {
            pointer: pointer.clone(),
            keywords: cause
                .kind
                .map_or_else(Vec::new, |kind| family_keywords(kind, value)),
        },
        None => match &cause.origin {
            Some(origin) => resolve_cause(origin, enclosing, enclosing_value, located),
            None => Cause {
                pointer: enclosing.to_string(),
                keywords: family_keywords(
                    cause.kind.unwrap_or(parse::PartKind::Branch),
                    enclosing_value,
                ),
            },
        },
    }
}

/// The family's keywords the object holds, in family order.
fn family_keywords(kind: parse::PartKind, value: &Value) -> Vec<String> {
    let map = value.as_object().expect("a cause names a schema object");
    kind.keywords()
        .iter()
        .filter(|keyword| map.contains_key(**keyword))
        .map(|keyword| (*keyword).to_string())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::{options, PreparedDocument};
    use serde_json::{json, Value};

    /// Every pointer in `document`, deepest first.
    fn every_pointer(document: &Value) -> Vec<String> {
        fn walk(value: &Value, pointer: &str, out: &mut Vec<String>) {
            match value {
                Value::Object(map) => {
                    for (key, child) in map {
                        walk(child, &format!("{pointer}/{key}"), out);
                    }
                }
                Value::Array(items) => {
                    for (index, child) in items.iter().enumerate() {
                        walk(child, &format!("{pointer}/{index}"), out);
                    }
                }
                Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
            }
            out.push(pointer.to_string());
        }
        let mut out = Vec::new();
        walk(document, "", &mut out);
        out
    }

    /// A prepared document holding no bodies yet, whose first read parses every definition itself.
    fn unseeded(document: &Value) -> PreparedDocument<'_> {
        options().prepare(document).expect("prepares")
    }

    fn documents() -> Vec<Value> {
        vec![
            json!({
                "$defs": {"Named": {"type": "object", "required": ["name"]}},
                "properties": {"a": {"$ref": "#/$defs/Named"}},
                "allOf": [{"$ref": "#/$defs/Named"}]
            }),
            // A body reached only through another body.
            json!({
                "$defs": {
                    "Inner": {"type": "integer", "minimum": 5},
                    "Outer": {"allOf": [{"$ref": "#/$defs/Inner"}, {"maximum": 3}]}
                },
                "properties": {"a": {"$ref": "#/$defs/Outer"}}
            }),
            // A cycle, which the definition fixpoint has to settle.
            json!({
                "$defs": {"Node": {
                    "type": "object",
                    "properties": {"next": {"$ref": "#/$defs/Node"}}
                }},
                "$ref": "#/$defs/Node"
            }),
            // A subresource carrying its own `$id`, so keys are generated against another base.
            json!({
                "$defs": {"Sub": {
                    "$id": "https://example.com/sub",
                    "$defs": {"Leaf": {"type": "string"}},
                    "properties": {"leaf": {"$ref": "#/$defs/Leaf"}}
                }},
                "properties": {"s": {"$ref": "https://example.com/sub"}}
            }),
            // A dynamic reference, where a key is specialized by the scope it was reached through.
            json!({
                "$defs": {"Items": {
                    "$dynamicAnchor": "T",
                    "type": "array",
                    "items": {"$dynamicRef": "#T"}
                }},
                "properties": {"a": {"$ref": "#/$defs/Items"}}
            }),
            // A definition the document's own root stops referencing once it folds.
            json!({
                "$defs": {"Dead": {"allOf": [{"type": "string"}, {"type": "integer"}]}},
                "properties": {"a": {"$ref": "#/$defs/Dead"}}
            }),
        ]
    }

    #[test]
    fn a_seeded_selection_reads_the_same_as_one_that_parses_its_own_definitions() {
        for document in documents() {
            let seeded = options().prepare(&document).expect("prepares");
            // Primed by reading the whole document, which leaves every body it reached behind.
            let _ = seeded.canonicalize();
            for pointer in every_pointer(&document) {
                let left = seeded.canonicalize_at(&pointer).map(|s| s.to_json_schema());
                // Prepared afresh, so this read holds no body and parses each one it reaches.
                let right = unseeded(&document)
                    .canonicalize_at(&pointer)
                    .map(|s| s.to_json_schema());
                assert_eq!(
                    left.as_ref().ok(),
                    right.as_ref().ok(),
                    "{pointer} of {document}"
                );
                assert_eq!(left.is_err(), right.is_err(), "{pointer} of {document}");
            }
        }
    }

    #[test]
    fn a_seeded_document_reads_the_same_as_one_that_parses_it_alone() {
        for document in documents() {
            let seeded = options().prepare(&document).expect("prepares");
            // Primed by reading every subschema, so the bodies the document read reuses were
            // parsed for another target.
            for pointer in every_pointer(&document) {
                let _ = seeded.canonicalize_at(&pointer);
            }
            assert_eq!(
                seeded
                    .canonicalize()
                    .expect("canonicalizes")
                    .to_json_schema(),
                unseeded(&document)
                    .canonicalize()
                    .expect("canonicalizes")
                    .to_json_schema(),
                "{document}"
            );
        }
    }

    #[test]
    fn reading_the_same_subschema_again_reads_the_same() {
        for document in documents() {
            let prepared = options().prepare(&document).expect("prepares");
            for pointer in every_pointer(&document) {
                let first = prepared
                    .canonicalize_at(&pointer)
                    .map(|s| s.to_json_schema());
                let again = prepared
                    .canonicalize_at(&pointer)
                    .map(|s| s.to_json_schema());
                assert_eq!(first.ok(), again.ok(), "{pointer} of {document}");
            }
        }
    }
}