jsonschema-schema 0.1.0

Typed Rust representation of JSON Schema draft 2020-12
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
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::ext_lintel::LintelExt;
use crate::ext_taplo::TaploSchemaExt;

/// A JSON Schema value — either a boolean schema or an object schema.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SchemaValue {
    Bool(bool),
    Schema(Box<Schema>),
}

/// JSON Schema `type` keyword — single type string or union array.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TypeValue {
    Single(String),
    Union(Vec<String>),
}

/// A JSON Schema object (draft 2020-12).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Schema {
    // --- Core identifiers ---
    #[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,
    #[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(rename = "$ref", skip_serializing_if = "Option::is_none")]
    pub ref_: Option<String>,
    #[serde(rename = "$anchor", skip_serializing_if = "Option::is_none")]
    pub anchor: Option<String>,
    #[serde(rename = "$dynamicRef", skip_serializing_if = "Option::is_none")]
    pub dynamic_ref: Option<String>,
    #[serde(rename = "$dynamicAnchor", skip_serializing_if = "Option::is_none")]
    pub dynamic_anchor: Option<String>,
    #[serde(rename = "$comment", skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    #[serde(rename = "$defs", skip_serializing_if = "Option::is_none")]
    pub defs: Option<IndexMap<String, SchemaValue>>,

    // --- Metadata ---
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(
        rename = "markdownDescription",
        skip_serializing_if = "Option::is_none"
    )]
    pub markdown_description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<bool>,
    #[serde(rename = "readOnly", skip_serializing_if = "Option::is_none")]
    pub read_only: Option<bool>,
    #[serde(rename = "writeOnly", skip_serializing_if = "Option::is_none")]
    pub write_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub examples: Option<Vec<Value>>,

    // --- Type ---
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub type_: Option<TypeValue>,
    #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
    pub enum_: Option<Vec<Value>>,
    #[serde(rename = "const", skip_serializing_if = "Option::is_none")]
    pub const_: Option<Value>,

    // --- Object ---
    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<IndexMap<String, SchemaValue>>,
    #[serde(rename = "patternProperties", skip_serializing_if = "Option::is_none")]
    pub pattern_properties: Option<IndexMap<String, SchemaValue>>,
    #[serde(
        rename = "additionalProperties",
        skip_serializing_if = "Option::is_none"
    )]
    pub additional_properties: Option<Box<SchemaValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,
    #[serde(rename = "propertyNames", skip_serializing_if = "Option::is_none")]
    pub property_names: Option<Box<SchemaValue>>,
    #[serde(rename = "minProperties", skip_serializing_if = "Option::is_none")]
    pub min_properties: Option<u64>,
    #[serde(rename = "maxProperties", skip_serializing_if = "Option::is_none")]
    pub max_properties: Option<u64>,
    #[serde(
        rename = "unevaluatedProperties",
        skip_serializing_if = "Option::is_none"
    )]
    pub unevaluated_properties: Option<Box<SchemaValue>>,

    // --- Array ---
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<SchemaValue>>,
    #[serde(rename = "prefixItems", skip_serializing_if = "Option::is_none")]
    pub prefix_items: Option<Vec<SchemaValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contains: Option<Box<SchemaValue>>,
    #[serde(rename = "minContains", skip_serializing_if = "Option::is_none")]
    pub min_contains: Option<u64>,
    #[serde(rename = "maxContains", skip_serializing_if = "Option::is_none")]
    pub max_contains: Option<u64>,
    #[serde(rename = "minItems", skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u64>,
    #[serde(rename = "maxItems", skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u64>,
    #[serde(rename = "uniqueItems", skip_serializing_if = "Option::is_none")]
    pub unique_items: Option<bool>,
    #[serde(rename = "unevaluatedItems", skip_serializing_if = "Option::is_none")]
    pub unevaluated_items: Option<Box<SchemaValue>>,

    // --- Number ---
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum: Option<Value>,
    #[serde(rename = "exclusiveMinimum", skip_serializing_if = "Option::is_none")]
    pub exclusive_minimum: Option<Value>,
    #[serde(rename = "exclusiveMaximum", skip_serializing_if = "Option::is_none")]
    pub exclusive_maximum: Option<Value>,
    #[serde(rename = "multipleOf", skip_serializing_if = "Option::is_none")]
    pub multiple_of: Option<Value>,

    // --- String ---
    #[serde(rename = "minLength", skip_serializing_if = "Option::is_none")]
    pub min_length: Option<u64>,
    #[serde(rename = "maxLength", skip_serializing_if = "Option::is_none")]
    pub max_length: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pattern: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,

    // --- Composition ---
    #[serde(rename = "allOf", skip_serializing_if = "Option::is_none")]
    pub all_of: Option<Vec<SchemaValue>>,
    #[serde(rename = "anyOf", skip_serializing_if = "Option::is_none")]
    pub any_of: Option<Vec<SchemaValue>>,
    #[serde(rename = "oneOf", skip_serializing_if = "Option::is_none")]
    pub one_of: Option<Vec<SchemaValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub not: Option<Box<SchemaValue>>,

    // --- Conditional ---
    #[serde(rename = "if", skip_serializing_if = "Option::is_none")]
    pub if_: Option<Box<SchemaValue>>,
    #[serde(rename = "then", skip_serializing_if = "Option::is_none")]
    pub then_: Option<Box<SchemaValue>>,
    #[serde(rename = "else", skip_serializing_if = "Option::is_none")]
    pub else_: Option<Box<SchemaValue>>,

    // --- Dependencies (2020-12) ---
    #[serde(rename = "dependentRequired", skip_serializing_if = "Option::is_none")]
    pub dependent_required: Option<IndexMap<String, Vec<String>>>,
    #[serde(rename = "dependentSchemas", skip_serializing_if = "Option::is_none")]
    pub dependent_schemas: Option<IndexMap<String, SchemaValue>>,

    // --- Content ---
    #[serde(rename = "contentMediaType", skip_serializing_if = "Option::is_none")]
    pub content_media_type: Option<String>,
    #[serde(rename = "contentEncoding", skip_serializing_if = "Option::is_none")]
    pub content_encoding: Option<String>,
    #[serde(rename = "contentSchema", skip_serializing_if = "Option::is_none")]
    pub content_schema: Option<Box<SchemaValue>>,

    // --- Extensions ---
    #[serde(rename = "x-taplo", skip_serializing_if = "Option::is_none")]
    pub x_taplo: Option<TaploSchemaExt>,
    #[serde(rename = "x-taplo-info", skip_serializing_if = "Option::is_none")]
    pub x_taplo_info: Option<Value>,
    #[serde(rename = "x-lintel", skip_serializing_if = "Option::is_none")]
    pub x_lintel: Option<LintelExt>,
    #[serde(
        rename = "x-tombi-toml-version",
        skip_serializing_if = "Option::is_none"
    )]
    pub x_tombi_toml_version: Option<String>,
    #[serde(
        rename = "x-tombi-table-keys-order",
        skip_serializing_if = "Option::is_none"
    )]
    pub x_tombi_table_keys_order: Option<Value>,
    #[serde(
        rename = "x-tombi-additional-key-label",
        skip_serializing_if = "Option::is_none"
    )]
    pub x_tombi_additional_key_label: Option<String>,
    #[serde(
        rename = "x-tombi-array-values-order",
        skip_serializing_if = "Option::is_none"
    )]
    pub x_tombi_array_values_order: Option<Value>,

    // --- Catch-all for unknown properties ---
    #[serde(flatten)]
    pub extra: IndexMap<String, Value>,
}

impl SchemaValue {
    /// Get the inner `Schema` if this is an object schema, `None` for bool schemas.
    pub fn as_schema(&self) -> Option<&Schema> {
        match self {
            Self::Schema(s) => Some(s),
            Self::Bool(_) => None,
        }
    }
}

impl Schema {
    /// Parse from a `serde_json::Value` without migration.
    ///
    /// # Errors
    ///
    /// Returns an error if the value cannot be deserialized into a `Schema`.
    pub fn from_value(value: Value) -> Result<Self, serde_json::Error> {
        serde_json::from_value(value)
    }

    /// Get the best description text, preferring `markdownDescription`.
    pub fn description(&self) -> Option<&str> {
        self.markdown_description
            .as_deref()
            .or(self.description.as_deref())
    }

    /// Get the required fields, or an empty slice.
    pub fn required_set(&self) -> &[String] {
        self.required.as_deref().unwrap_or_default()
    }

    /// Whether this schema is deprecated.
    pub fn is_deprecated(&self) -> bool {
        self.deprecated.unwrap_or(false)
    }

    /// Produce a short human-readable type string.
    pub fn type_str(&self) -> Option<String> {
        schema_type_str(self)
    }

    /// Look up a schema-keyword field by its JSON key name.
    ///
    /// Returns a reference to the `SchemaValue` stored under that keyword,
    /// or `None` if the field is absent.
    pub fn get_keyword(&self, key: &str) -> Option<&SchemaValue> {
        match key {
            "items" => self.items.as_deref(),
            "contains" => self.contains.as_deref(),
            "additionalProperties" => self.additional_properties.as_deref(),
            "propertyNames" => self.property_names.as_deref(),
            "unevaluatedProperties" => self.unevaluated_properties.as_deref(),
            "unevaluatedItems" => self.unevaluated_items.as_deref(),
            "not" => self.not.as_deref(),
            "if" => self.if_.as_deref(),
            "then" => self.then_.as_deref(),
            "else" => self.else_.as_deref(),
            "contentSchema" => self.content_schema.as_deref(),
            _ => None,
        }
    }

    /// Look up a named child within a keyword that holds a map of schemas.
    ///
    /// For example, `get_map_entry("properties", "name")` returns the schema
    /// for the `name` property.
    pub fn get_map_entry(&self, keyword: &str, key: &str) -> Option<&SchemaValue> {
        match keyword {
            "properties" => self.properties.as_ref()?.get(key),
            "patternProperties" => self.pattern_properties.as_ref()?.get(key),
            "$defs" => self.defs.as_ref()?.get(key),
            "dependentSchemas" => self.dependent_schemas.as_ref()?.get(key),
            _ => None,
        }
    }

    /// Look up an indexed child within a keyword that holds an array of schemas.
    pub fn get_array_entry(&self, keyword: &str, index: usize) -> Option<&SchemaValue> {
        match keyword {
            "allOf" => self.all_of.as_ref()?.get(index),
            "anyOf" => self.any_of.as_ref()?.get(index),
            "oneOf" => self.one_of.as_ref()?.get(index),
            "prefixItems" => self.prefix_items.as_ref()?.get(index),
            _ => None,
        }
    }
}

/// Produce a short human-readable type string for a schema.
fn schema_type_str(schema: &Schema) -> Option<String> {
    // Explicit type field
    if let Some(ref ty) = schema.type_ {
        return match ty {
            TypeValue::Single(s) if s == "array" => {
                let item_ty = schema
                    .items
                    .as_ref()
                    .and_then(|sv| sv.as_schema())
                    .and_then(schema_type_str);
                match item_ty {
                    Some(item_ty) => Some(format!("{item_ty}[]")),
                    None => Some("array".to_string()),
                }
            }
            TypeValue::Single(s) => Some(s.clone()),
            TypeValue::Union(arr) => Some(arr.join(" | ")),
        };
    }

    // $ref
    if let Some(ref r) = schema.ref_ {
        return Some(ref_name(r).to_string());
    }

    // oneOf/anyOf
    for variants in [&schema.one_of, &schema.any_of].into_iter().flatten() {
        let types: Vec<String> = variants
            .iter()
            .filter_map(|v| match v {
                SchemaValue::Schema(s) => {
                    schema_type_str(s).or_else(|| s.ref_.as_ref().map(|r| ref_name(r).to_string()))
                }
                SchemaValue::Bool(_) => None,
            })
            .collect();
        if !types.is_empty() {
            return Some(types.join(" | "));
        }
    }

    // const
    if let Some(ref c) = schema.const_ {
        return Some(format!("const: {c}"));
    }

    // enum
    if schema.enum_.is_some() {
        return Some("enum".to_string());
    }

    None
}

/// Extract the trailing name from a `$ref` path (e.g. `"#/$defs/Foo"` -> `"Foo"`).
pub fn ref_name(ref_str: &str) -> &str {
    ref_str.rsplit('/').next().unwrap_or(ref_str)
}

/// Resolve a `$ref` within the same schema document.
///
/// If the given schema has a `$ref` that begins with `#/`, follow the path
/// through the root schema. Otherwise return the schema unchanged.
pub fn resolve_ref<'a>(schema: &'a Schema, root: &'a Schema) -> &'a Schema {
    if let Some(ref ref_str) = schema.ref_
        && let Some(path) = ref_str.strip_prefix("#/")
    {
        // Navigate the root using serde_json::Value for flexibility
        let Ok(root_value) = serde_json::to_value(root) else {
            return schema;
        };
        let mut current = &root_value;
        for segment in path.split('/') {
            let decoded = segment.replace("~1", "/").replace("~0", "~");
            match current.get(&decoded) {
                Some(next) => current = next,
                None => return schema,
            }
        }
        // Try to deserialize the resolved value back into a Schema.
        // This is expensive, so we use a different approach for the explain crate.
        // For now, just return the original schema — the explain crate has its own
        // resolve_ref that works with SchemaValue trees directly.
        let _ = current;
        return schema;
    }
    schema
}

/// Walk a JSON Pointer path through a schema, resolving `$ref` at each step.
///
/// Segments are decoded per RFC 6901 (`~1` → `/`, `~0` → `~`).
/// Returns the sub-`SchemaValue` at the given pointer, or an error.
///
/// # Errors
///
/// Returns an error if a segment in the pointer cannot be resolved.
pub fn navigate_pointer<'a>(
    schema: &'a SchemaValue,
    root: &'a SchemaValue,
    pointer: &str,
) -> Result<&'a SchemaValue, String> {
    let path = pointer.strip_prefix('/').unwrap_or(pointer);
    if path.is_empty() {
        return Ok(schema);
    }

    let mut current = resolve_schema_value_ref(schema, root);
    let mut segments = path.split('/').peekable();

    while let Some(segment) = segments.next() {
        let decoded = segment.replace("~1", "/").replace("~0", "~");
        current = resolve_schema_value_ref(current, root);

        let Some(schema) = current.as_schema() else {
            return Err(format!(
                "cannot resolve segment '{decoded}' in pointer '{pointer}'"
            ));
        };

        // Map-bearing keywords: consume this segment AND the next one.
        if is_map_keyword(&decoded) {
            let key_segment = segments
                .next()
                .ok_or_else(|| format!("expected key after '{decoded}' in pointer '{pointer}'"))?;
            let key = key_segment.replace("~1", "/").replace("~0", "~");
            if let Some(entry) = schema.get_map_entry(&decoded, &key) {
                current = entry;
                continue;
            }
            return Err(format!(
                "cannot resolve segment '{key}' in '{decoded}' in pointer '{pointer}'"
            ));
        }

        // Array-bearing keywords: consume this segment, then the next as an index.
        if is_array_keyword(&decoded) {
            let idx_segment = segments.next().ok_or_else(|| {
                format!("expected index after '{decoded}' in pointer '{pointer}'")
            })?;
            let idx: usize = idx_segment.parse().map_err(|_| {
                format!("expected numeric index after '{decoded}', got '{idx_segment}'")
            })?;
            if let Some(entry) = schema.get_array_entry(&decoded, idx) {
                current = entry;
                continue;
            }
            return Err(format!(
                "index {idx} out of bounds in '{decoded}' in pointer '{pointer}'"
            ));
        }

        // Single-value keywords (items, not, if, then, else, etc.)
        if let Some(sv) = schema.get_keyword(&decoded) {
            current = sv;
            continue;
        }

        // Fall back: try as a key in the schema's maps (for when the
        // pointer navigates directly into a map without naming the keyword).
        if let Some(sv) = schema.get_map_entry_by_pointer_segment(&decoded) {
            current = sv;
            continue;
        }

        // Try as array index (for arrays embedded in composition keywords)
        if let Ok(idx) = decoded.parse::<usize>() {
            let found = ["allOf", "anyOf", "oneOf", "prefixItems"]
                .iter()
                .find_map(|kw| schema.get_array_entry(kw, idx));
            if let Some(entry) = found {
                current = entry;
                continue;
            }
        }

        return Err(format!(
            "cannot resolve segment '{decoded}' in pointer '{pointer}'"
        ));
    }

    Ok(resolve_schema_value_ref(current, root))
}

/// Whether a JSON pointer segment names a map-bearing keyword.
fn is_map_keyword(segment: &str) -> bool {
    matches!(
        segment,
        "properties" | "patternProperties" | "$defs" | "dependentSchemas"
    )
}

/// Whether a JSON pointer segment names an array-bearing keyword.
fn is_array_keyword(segment: &str) -> bool {
    matches!(segment, "allOf" | "anyOf" | "oneOf" | "prefixItems")
}

/// Resolve `$ref` on a `SchemaValue`, returning the referenced `SchemaValue`.
fn resolve_schema_value_ref<'a>(sv: &'a SchemaValue, root: &'a SchemaValue) -> &'a SchemaValue {
    let Some(schema) = sv.as_schema() else {
        return sv;
    };
    if let Some(ref ref_str) = schema.ref_
        && let Some(path) = ref_str.strip_prefix("#/")
    {
        let mut current = root;
        let mut segments = path.split('/').peekable();
        while let Some(segment) = segments.next() {
            let decoded = segment.replace("~1", "/").replace("~0", "~");
            let Some(inner) = current.as_schema() else {
                return sv;
            };

            // Map-bearing keywords: consume the next segment as a key
            if is_map_keyword(&decoded) {
                let Some(key_segment) = segments.next() else {
                    return sv;
                };
                let key = key_segment.replace("~1", "/").replace("~0", "~");
                match inner.get_map_entry(&decoded, &key) {
                    Some(n) => current = n,
                    None => return sv,
                }
                continue;
            }

            // Array-bearing keywords: consume the next segment as an index
            if is_array_keyword(&decoded) {
                let Some(idx_segment) = segments.next() else {
                    return sv;
                };
                let Ok(idx) = idx_segment.parse::<usize>() else {
                    return sv;
                };
                match inner.get_array_entry(&decoded, idx) {
                    Some(n) => current = n,
                    None => return sv,
                }
                continue;
            }

            // Single-value keywords
            if let Some(n) = inner.get_keyword(&decoded) {
                current = n;
                continue;
            }

            // Fall back to map entry lookup
            if let Some(n) = inner.get_map_entry_by_pointer_segment(&decoded) {
                current = n;
                continue;
            }

            return sv;
        }
        return current;
    }
    sv
}

impl Schema {
    /// Look up a child by a JSON pointer segment name.
    /// This handles both map keywords (where the segment is a key within the map)
    /// and direct keywords.
    fn get_map_entry_by_pointer_segment(&self, segment: &str) -> Option<&SchemaValue> {
        // Try all map-bearing keyword fields.
        // For pointer navigation, when we're inside a "properties" object,
        // the segment is the property name.
        self.properties
            .as_ref()
            .and_then(|m| m.get(segment))
            .or_else(|| {
                self.pattern_properties
                    .as_ref()
                    .and_then(|m| m.get(segment))
            })
            .or_else(|| self.defs.as_ref().and_then(|m| m.get(segment)))
            .or_else(|| self.dependent_schemas.as_ref().and_then(|m| m.get(segment)))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn round_trip_simple_schema() {
        let json = json!({
            "type": "object",
            "title": "Test",
            "properties": {
                "name": { "type": "string" }
            }
        });
        let schema: Schema = serde_json::from_value(json.clone()).unwrap();
        assert_eq!(schema.title.as_deref(), Some("Test"));
        assert!(schema.properties.is_some());

        let back = serde_json::to_value(&schema).unwrap();
        assert_eq!(back["type"], "object");
        assert_eq!(back["title"], "Test");
    }

    #[test]
    fn bool_schema_value() {
        let json = json!(true);
        let sv: SchemaValue = serde_json::from_value(json).unwrap();
        assert!(matches!(sv, SchemaValue::Bool(true)));
        assert!(sv.as_schema().is_none());
    }

    #[test]
    fn schema_value_object() {
        let json = json!({"type": "string"});
        let sv: SchemaValue = serde_json::from_value(json).unwrap();
        let s = sv.as_schema().unwrap();
        assert!(matches!(s.type_, Some(TypeValue::Single(ref t)) if t == "string"));
    }

    #[test]
    fn type_value_single() {
        let json = json!("string");
        let tv: TypeValue = serde_json::from_value(json).unwrap();
        assert!(matches!(tv, TypeValue::Single(ref s) if s == "string"));
    }

    #[test]
    fn type_value_union() {
        let json = json!(["string", "null"]);
        let tv: TypeValue = serde_json::from_value(json).unwrap();
        assert!(matches!(tv, TypeValue::Union(ref v) if v.len() == 2));
    }

    #[test]
    fn description_prefers_markdown() {
        let schema = Schema {
            description: Some("plain".into()),
            markdown_description: Some("**rich**".into()),
            ..Default::default()
        };
        assert_eq!(schema.description(), Some("**rich**"));
    }

    #[test]
    fn description_falls_back() {
        let schema = Schema {
            description: Some("plain".into()),
            ..Default::default()
        };
        assert_eq!(schema.description(), Some("plain"));
    }

    #[test]
    fn type_str_simple() {
        let schema = Schema {
            type_: Some(TypeValue::Single("string".into())),
            ..Default::default()
        };
        assert_eq!(schema.type_str().as_deref(), Some("string"));
    }

    #[test]
    fn type_str_union() {
        let schema = Schema {
            type_: Some(TypeValue::Union(vec!["string".into(), "null".into()])),
            ..Default::default()
        };
        assert_eq!(schema.type_str().as_deref(), Some("string | null"));
    }

    #[test]
    fn type_str_array_with_items() {
        let items = SchemaValue::Schema(Box::new(Schema {
            type_: Some(TypeValue::Single("string".into())),
            ..Default::default()
        }));
        let schema = Schema {
            type_: Some(TypeValue::Single("array".into())),
            items: Some(Box::new(items)),
            ..Default::default()
        };
        assert_eq!(schema.type_str().as_deref(), Some("string[]"));
    }

    #[test]
    fn type_str_ref() {
        let schema = Schema {
            ref_: Some("#/$defs/Foo".into()),
            ..Default::default()
        };
        assert_eq!(schema.type_str().as_deref(), Some("Foo"));
    }

    #[test]
    fn is_deprecated_default_false() {
        let schema = Schema::default();
        assert!(!schema.is_deprecated());
    }

    #[test]
    fn is_deprecated_true() {
        let schema = Schema {
            deprecated: Some(true),
            ..Default::default()
        };
        assert!(schema.is_deprecated());
    }

    #[test]
    fn required_set_empty() {
        let schema = Schema::default();
        assert!(schema.required_set().is_empty());
    }

    #[test]
    fn required_set_values() {
        let schema = Schema {
            required: Some(vec!["a".into(), "b".into()]),
            ..Default::default()
        };
        assert_eq!(schema.required_set(), &["a", "b"]);
    }

    #[test]
    fn extra_fields_preserved() {
        let json = json!({
            "type": "object",
            "x-custom": "value",
            "x-another": 42
        });
        let schema: Schema = serde_json::from_value(json).unwrap();
        assert_eq!(schema.extra.get("x-custom").unwrap(), "value");
        assert_eq!(schema.extra.get("x-another").unwrap(), 42);
    }

    #[test]
    fn x_taplo_deserialization() {
        let json = json!({
            "type": "object",
            "x-taplo": {
                "hidden": true,
                "docs": {
                    "main": "Main docs"
                }
            }
        });
        let schema: Schema = serde_json::from_value(json).unwrap();
        let taplo = schema.x_taplo.unwrap();
        assert_eq!(taplo.hidden, Some(true));
        assert_eq!(taplo.docs.unwrap().main.as_deref(), Some("Main docs"));
    }

    #[test]
    fn x_lintel_deserialization() {
        let json = json!({
            "type": "object",
            "x-lintel": {
                "source": "https://example.com/schema.json",
                "sourceSha256": "abc123"
            }
        });
        let schema: Schema = serde_json::from_value(json).unwrap();
        let lintel = schema.x_lintel.unwrap();
        assert_eq!(
            lintel.source.as_deref(),
            Some("https://example.com/schema.json")
        );
        assert_eq!(lintel.source_sha256.as_deref(), Some("abc123"));
    }

    #[test]
    fn navigate_pointer_empty() {
        let sv = SchemaValue::Schema(Box::new(Schema {
            type_: Some(TypeValue::Single("object".into())),
            ..Default::default()
        }));
        let result = navigate_pointer(&sv, &sv, "").unwrap();
        assert!(result.as_schema().is_some());
    }

    #[test]
    fn navigate_pointer_properties() {
        let name_schema = SchemaValue::Schema(Box::new(Schema {
            type_: Some(TypeValue::Single("string".into())),
            ..Default::default()
        }));
        let mut props = IndexMap::new();
        props.insert("name".into(), name_schema);
        let root = SchemaValue::Schema(Box::new(Schema {
            properties: Some(props),
            ..Default::default()
        }));
        let result = navigate_pointer(&root, &root, "/properties/name").unwrap();
        let s = result.as_schema().unwrap();
        assert!(matches!(s.type_, Some(TypeValue::Single(ref t)) if t == "string"));
    }

    #[test]
    fn navigate_pointer_resolves_ref() {
        let item_schema = SchemaValue::Schema(Box::new(Schema {
            type_: Some(TypeValue::Single("object".into())),
            description: Some("An item".into()),
            ..Default::default()
        }));
        let ref_schema = SchemaValue::Schema(Box::new(Schema {
            ref_: Some("#/$defs/Item".into()),
            ..Default::default()
        }));
        let mut defs = IndexMap::new();
        defs.insert("Item".into(), item_schema);
        let mut props = IndexMap::new();
        props.insert("item".into(), ref_schema);
        let root = SchemaValue::Schema(Box::new(Schema {
            properties: Some(props),
            defs: Some(defs),
            ..Default::default()
        }));
        let result = navigate_pointer(&root, &root, "/properties/item").unwrap();
        let s = result.as_schema().unwrap();
        assert_eq!(s.description.as_deref(), Some("An item"));
    }

    #[test]
    fn navigate_pointer_bad_segment_errors() {
        let sv = SchemaValue::Schema(Box::default());
        let err = navigate_pointer(&sv, &sv, "/nonexistent").unwrap_err();
        assert!(err.contains("nonexistent"));
    }

    #[test]
    fn parse_cargo_fixture() {
        let content =
            std::fs::read_to_string("../jsonschema-migrate/tests/fixtures/cargo.json").unwrap();
        let value: Value = serde_json::from_str(&content).unwrap();
        let mut migrated = value;
        jsonschema_migrate::migrate_to_2020_12(&mut migrated);
        let schema: Schema = serde_json::from_value(migrated).unwrap();
        assert!(schema.title.is_some() || schema.type_.is_some());
        // Verify x-taplo is parsed if present
        if schema.x_taplo.is_some() {
            // Just verify it parsed without error
        }
    }
}