modelcontextprotocol 0.2.4

Filesystem, Fetch, Memory, Shell, Skills, and Agents MCP servers in a single binary, implementing the Model Context Protocol 2026-07-28 specification.
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
//! Deterministic MCP input-schema → OpenRouter function-parameter schema
//! normalizer and a small local JSON-schema validator.
//!
//! This module is the single source of truth for schema translation used by
//! the OpenRouter e2e suite: every runtime `Tool` definition is normalized
//! here, and model-generated arguments are validated against the normalized
//! schema before they are ever passed to an MCP server.
//!
//! All tests in this module are offline and use local JSON fixtures only.

use serde_json::{Map, Value};

/// Result of normalizing one MCP tool schema.
#[derive(Debug, Clone, Default)]
pub struct Normalized {
    /// The OpenRouter-parameter schema (JSON Schema, `type: object` at the
    /// root, no `$schema`/`$defs`/`$ref` left behind).
    pub schema: Value,
    /// Human-readable diagnostics for every unsupported construct that was
    /// dropped or simplified. Empty when the schema used only supported
    /// constructs.
    pub diagnostics: Vec<String>,
}

/// Keywords handled explicitly by the normalizer. Everything else that
/// appears in a schema node is recorded as an unsupported diagnostic and
/// dropped, so a model never sees a construct we cannot validate.
const SUPPORTED: &[&str] = &[
    "type",
    "properties",
    "required",
    "items",
    "description",
    "default",
    "enum",
    "minimum",
    "maximum",
    "minItems",
    "maxItems",
    "additionalProperties",
    "format",
];

/// Keywords that are silently consumed (JSON Schema meta / reference
/// machinery) rather than reported as unsupported.
const META: &[&str] = &["$schema", "$defs", "title", "$ref"];

/// Normalize an MCP tool input schema (as produced by
/// `rmcp::model::Tool::schema_as_json_value`) into a function-parameters
/// schema for OpenRouter.
///
/// Guarantees:
/// - the result is always `type: object` with a `properties` map;
/// - `$defs`/`$ref` are resolved recursively (local `#/$defs/NAME` refs);
/// - nullable `type: ["T", "null"]` collapses to `T`;
/// - `properties`, `required`, `description`, `default`, `enum`,
///   `minimum`, `maximum`, `minItems`, `maxItems` and declared
///   `additionalProperties` are preserved verbatim;
/// - every object node declares `additionalProperties: false` unless the
///   schema already declared it, so a model cannot invent parameters;
/// - any other keyword is dropped and reported in `diagnostics`.
pub fn normalize_tool_schema(mcp: &Value) -> Normalized {
    let defs = match mcp.get("$defs").and_then(Value::as_object) {
        Some(defs) => defs.clone(),
        None => Map::new(),
    };
    let mut normalized = Normalized::default();
    let mut root = normalize_node(mcp, &defs, "$", &mut normalized);
    match root {
        Value::Object(_) => {}
        _ => {
            normalized
                .diagnostics
                .push("$: schema root is not an object; replaced with {}.".to_string());
            root = Value::Object(Map::new());
        }
    }
    let obj = root.as_object_mut().expect("root is an object");
    if !obj.contains_key("type") {
        obj.insert("type".to_string(), Value::String("object".to_string()));
        normalized
            .diagnostics
            .push("$: missing type at root; assumed object.".to_string());
    }
    if !obj.contains_key("properties") {
        obj.insert("properties".to_string(), Value::Object(Map::new()));
    }
    // Root-level additionalProperties policy.
    match obj.get("additionalProperties") {
        None => {
            obj.insert("additionalProperties".to_string(), Value::Bool(false));
        }
        Some(Value::Bool(true)) => {
            normalized.diagnostics.push(
                "$: additionalProperties: true at root is preserved but weakens the ".to_string()
                    + "argument contract.",
            );
        }
        _ => {}
    }
    normalized.schema = Value::Object(obj.clone());
    normalized
}

/// Normalize one schema node, resolving `$defs` refs and recursing into
/// object/array constructs.
fn normalize_node(
    node: &Value,
    defs: &Map<String, Value>,
    path: &str,
    out: &mut Normalized,
) -> Value {
    let Some(obj) = node.as_object() else {
        // Non-object schema nodes (e.g. bare `true`) are left untouched.
        return node.clone();
    };
    let mut result = Map::new();

    // Resolve local refs first; anything else is unsupported.
    if let Some(reference) = obj.get("$ref").and_then(Value::as_str) {
        if let Some(name) = reference.strip_prefix("#/$defs/") {
            if let Some(target) = defs.get(name) {
                return normalize_node(target, defs, &format!("{path}.$ref:{name}"), out);
            }
            out.diagnostics.push(format!(
                "{path}: unresolvable $ref {reference:?}; node dropped."
            ));
            return Value::Object(Map::new());
        }
        out.diagnostics.push(format!(
            "{path}: non-local $ref {reference:?} is unsupported; node dropped."
        ));
        return Value::Object(Map::new());
    }

    // Collapse nullable type arrays: ["T", "null"] → T.
    if let Some(types) = obj.get("type").and_then(Value::as_array) {
        let non_null: Vec<&str> = types
            .iter()
            .filter_map(Value::as_str)
            .filter(|t| *t != "null")
            .collect();
        if non_null.len() == 1 {
            result.insert("type".to_string(), Value::String(non_null[0].to_string()));
        } else {
            if !non_null.is_empty() {
                out.diagnostics.push(format!(
                    "{path}: type array {types:?} is unsupported (only [T, null] is handled); \
                     keeping the first non-null type."
                ));
            }
            if let Some(first) = non_null.first() {
                result.insert("type".to_string(), Value::String(first.to_string()));
            }
        }
    }

    for (key, value) in obj {
        if SUPPORTED.contains(&key.as_str()) {
            if key == "type" {
                // Array form was already collapsed above; a scalar type is
                // inserted here. Never overwrite a collapsed result.
                if !result.contains_key("type") {
                    result.insert("type".to_string(), value.clone());
                }
                continue;
            }
            let normalized_value = match key.as_str() {
                "properties" => normalize_object_map(value, defs, path, out),
                "items" | "additionalProperties" => {
                    normalize_node(value, defs, &format!("{path}.{key}"), out)
                }
                _ => value.clone(),
            };
            result.insert(key.clone(), normalized_value);
        } else if !META.contains(&key.as_str()) {
            out.diagnostics
                .push(format!("{path}: unsupported keyword {key:?} dropped."));
        }
    }

    // additionalProperties policy for every object node.
    if result.get("type").and_then(Value::as_str) == Some("object")
        || result.contains_key("properties")
    {
        match result.get("additionalProperties") {
            None => {
                result.insert("additionalProperties".to_string(), Value::Bool(false));
            }
            Some(Value::Bool(true)) => {
                out.diagnostics.push(format!(
                    "{path}: additionalProperties: true is preserved but weakens the argument                      contract."
                ));
            }
            _ => {}
        }
    }

    Value::Object(result)
}

fn normalize_object_map(
    map: &Value,
    defs: &Map<String, Value>,
    path: &str,
    out: &mut Normalized,
) -> Value {
    let Some(object) = map.as_object() else {
        out.diagnostics
            .push(format!("{path}: properties is not an object; dropped."));
        return Value::Object(Map::new());
    };
    let mut result = Map::new();
    for (name, schema) in object {
        result.insert(
            name.clone(),
            normalize_node(schema, defs, &format!("{path}.properties.{name}"), out),
        );
    }
    Value::Object(result)
}

// ---------------------------------------------------------------------------
// Validator
// ---------------------------------------------------------------------------

/// Validate `value` against a (normalized) JSON schema node.
///
/// Deterministic and dependency-free. Implements the subset of JSON Schema
/// that all repository schemas need after normalization: `required`,
/// JSON types (including `["T", "null"]` arrays), object properties with
/// `additionalProperties` (bool or schema), array `items` with
/// `minItems`/`maxItems`, `enum`, and numeric `minimum`/`maximum`.
/// `anyOf`/`oneOf` are honored as union (first matching branch wins) so raw
/// un-normalized fixtures can also be validated. Unknown keywords are
/// ignored.
///
/// Returns every violation found (never stops at the first one), or `Ok`
/// when the value satisfies the schema.
pub fn validate(value: &Value, schema: &Value) -> Result<(), Vec<String>> {
    let mut errors = Vec::new();
    validate_node(value, schema, "$", &mut errors);
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

fn validate_node(value: &Value, schema: &Value, path: &str, errors: &mut Vec<String>) {
    let Some(node) = schema.as_object() else {
        // Bare boolean or otherwise unconstrained schema: accept.
        return;
    };

    // Union semantics for un-normalized fixtures.
    if let Some(branches) = node
        .get("anyOf")
        .or_else(|| node.get("oneOf"))
        .and_then(Value::as_array)
    {
        if branches.iter().any(|branch| {
            let mut branch_errors = Vec::new();
            validate_node(value, branch, path, &mut branch_errors);
            branch_errors.is_empty()
        }) {
            return;
        }
        errors.push(format!(
            "{path}: value does not match any union branch: {}",
            describe(value)
        ));
        return;
    }

    if let Some(types) = node.get("type")
        && !matches_type(value, types)
    {
        errors.push(format!(
            "{path}: expected {}, got {}",
            describe_types(types),
            describe(value)
        ));
        // Type mismatches short-circuit deeper checks for this node.
        return;
    }

    if let Some(enum_values) = node.get("enum").and_then(Value::as_array)
        && !enum_values.contains(value)
    {
        errors.push(format!(
            "{path}: value {} is not one of {}",
            describe(value),
            serde_json::to_string(&enum_values).unwrap_or_default()
        ));
    }

    if let Some(minimum) = node.get("minimum").and_then(Value::as_f64)
        && let Some(number) = value.as_f64()
        && number < minimum
    {
        errors.push(format!("{path}: {number} is below minimum {minimum}"));
    }
    if let Some(maximum) = node.get("maximum").and_then(Value::as_f64)
        && let Some(number) = value.as_f64()
        && number > maximum
    {
        errors.push(format!("{path}: {number} is above maximum {maximum}"));
    }

    match value {
        Value::Object(fields) => {
            if let Some(required) = node.get("required").and_then(Value::as_array) {
                for name in required {
                    if let Some(name) = name.as_str()
                        && !fields.contains_key(name)
                    {
                        errors.push(format!("{path}: missing required property {name:?}"));
                    }
                }
            }
            if let Some(properties) = node.get("properties").and_then(Value::as_object) {
                for (name, subschema) in properties {
                    if let Some(child) = fields.get(name) {
                        validate_node(child, subschema, &format!("{path}.{name}"), errors);
                    }
                }
            }
            match node.get("additionalProperties") {
                Some(Value::Bool(false)) => {
                    for name in fields.keys() {
                        let declared = properties_declare(node, name);
                        if !declared {
                            errors.push(format!(
                                "{path}: unexpected property {name:?} (additionalProperties is                                  false)"
                            ));
                        }
                    }
                }
                Some(Value::Bool(true)) | None => {}
                Some(subschema @ Value::Object(_)) => {
                    for (name, child) in fields {
                        let declared = properties_declare(node, name);
                        if !declared {
                            validate_node(child, subschema, &format!("{path}.{name}"), errors);
                        }
                    }
                }
                Some(other) => errors.push(format!(
                    "{path}: unsupported additionalProperties value {other:?}"
                )),
            }
        }
        Value::Array(items) => {
            if let Some(min_items) = node.get("minItems").and_then(Value::as_u64)
                && (items.len() as u64) < min_items
            {
                errors.push(format!(
                    "{path}: expected at least {min_items} items, got {}",
                    items.len()
                ));
            }
            if let Some(max_items) = node.get("maxItems").and_then(Value::as_u64)
                && (items.len() as u64) > max_items
            {
                errors.push(format!(
                    "{path}: expected at most {max_items} items, got {}",
                    items.len()
                ));
            }
            if let Some(items_schema) = node.get("items") {
                for (index, item) in items.iter().enumerate() {
                    validate_node(item, items_schema, &format!("{path}[{index}]"), errors);
                }
            }
        }
        _ => {}
    }
}

fn properties_declare(node: &serde_json::Map<String, Value>, name: &str) -> bool {
    node.get("properties")
        .and_then(Value::as_object)
        .is_some_and(|properties| properties.contains_key(name))
}

fn matches_type(value: &Value, types: &Value) -> bool {
    let single = |t: &str| match t {
        "object" => value.is_object(),
        "array" => value.is_array(),
        "string" => value.is_string(),
        "boolean" => value.is_boolean(),
        "null" => value.is_null(),
        "integer" => value.as_i64().is_some() || value.as_u64().is_some(),
        "number" => value.is_number(),
        _ => true, // unknown type names do not constrain
    };
    match types {
        Value::String(t) => single(t),
        Value::Array(list) => list.iter().filter_map(Value::as_str).any(single),
        _ => true,
    }
}

fn describe_types(types: &Value) -> String {
    match types {
        Value::String(t) => format!("{t:?}"),
        Value::Array(list) => list
            .iter()
            .filter_map(Value::as_str)
            .map(|t| format!("{t:?}"))
            .collect::<Vec<_>>()
            .join(" or "),
        _ => "a valid JSON type".to_string(),
    }
}

fn describe(value: &Value) -> String {
    match value {
        Value::String(s) => {
            let shown: String = s.chars().take(40).collect();
            format!(
                "string {shown:?}{}",
                if s.chars().count() > 40 { "…" } else { "" }
            )
        }
        Value::Null => "null".to_string(),
        Value::Bool(b) => format!("boolean {b}"),
        Value::Number(n) => format!("number {n}"),
        Value::Array(a) => format!("array of {} items", a.len()),
        Value::Object(o) => format!("object with {} properties", o.len()),
    }
}

// ---------------------------------------------------------------------------
// Offline unit tests (local JSON fixtures only)
// ---------------------------------------------------------------------------

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

    fn norm(fixture: Value) -> Normalized {
        normalize_tool_schema(&fixture)
    }

    fn validate_ok(value: Value, schema: &Value) {
        match validate(&value, schema) {
            Ok(()) => {}
            Err(errors) => panic!("expected valid, got errors: {errors:?}"),
        }
    }

    fn validate_err(value: Value, schema: &Value, expected: &[&str]) {
        let errors = validate(&value, schema).expect_err("expected invalid");
        for needle in expected {
            assert!(
                errors.iter().any(|e| e.contains(needle)),
                "expected an error containing {needle:?} in {errors:?}"
            );
        }
    }

    // -- normalizer: fixtures ---------------------------------------------

    #[test]
    fn normalizes_plain_object_schema() {
        let fixture = json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "type": "object",
            "properties": {
                "path": { "type": "string", "description": "File path" },
                "head": {
                    "type": ["integer", "null"],
                    "default": null,
                    "format": "uint32",
                    "minimum": 0
                }
            },
            "required": ["path"]
        });
        let out = norm(fixture);
        assert!(out.diagnostics.is_empty(), "{:?}", out.diagnostics);
        let schema = out.schema;
        assert_eq!(schema["type"], "object");
        assert_eq!(schema["properties"]["path"]["type"], "string");
        assert_eq!(
            schema["properties"]["head"]["type"], "integer",
            "null collapsed"
        );
        assert_eq!(schema["properties"]["head"]["default"], Value::Null);
        assert_eq!(schema["properties"]["head"]["minimum"], 0);
        assert_eq!(schema["required"], json!(["path"]));
        assert_eq!(schema["additionalProperties"], false);
        assert!(!schema.as_object().unwrap().contains_key("$schema"));
        assert!(
            schema["properties"]["head"]
                .as_object()
                .unwrap()
                .get("additionalProperties")
                .is_none(),
            "scalar nodes carry no additionalProperties"
        );
    }

    #[test]
    fn resolves_local_defs_refs_nested() {
        let fixture = json!({
            "$defs": {
                "EditOperation": {
                    "type": "object",
                    "properties": {
                        "oldText": { "type": "string" },
                        "newText": { "type": "string" }
                    },
                    "required": ["oldText", "newText"]
                }
            },
            "type": "object",
            "properties": {
                "edits": {
                    "type": "array",
                    "items": { "$ref": "#/$defs/EditOperation" }
                }
            },
            "required": ["edits"]
        });
        let out = norm(fixture);
        assert!(out.diagnostics.is_empty(), "{:?}", out.diagnostics);
        let items = &out.schema["properties"]["edits"]["items"];
        assert_eq!(items["type"], "object");
        assert_eq!(items["properties"]["oldText"]["type"], "string");
        assert_eq!(items["required"], json!(["oldText", "newText"]));
        assert!(!out.schema.as_object().unwrap().contains_key("$defs"));
        assert!(!items.as_object().unwrap().contains_key("$ref"));
    }

    #[test]
    fn preserves_enums_min_max_and_list_bounds() {
        let fixture = json!({
            "type": "object",
            "properties": {
                "mode": {
                    "type": "string",
                    "enum": ["name", "size", "mtime"]
                },
                "count": { "type": "integer", "minimum": 1, "maximum": 99 },
                "tags": {
                    "type": "array",
                    "items": { "type": "string" },
                    "minItems": 1,
                    "maxItems": 5
                }
            },
            "required": ["mode", "count"]
        });
        let out = norm(fixture);
        assert!(out.diagnostics.is_empty(), "{:?}", out.diagnostics);
        assert_eq!(
            out.schema["properties"]["mode"]["enum"],
            json!(["name", "size", "mtime"])
        );
        assert_eq!(out.schema["properties"]["count"]["minimum"], 1);
        assert_eq!(out.schema["properties"]["count"]["maximum"], 99);
        assert_eq!(out.schema["properties"]["tags"]["minItems"], 1);
        assert_eq!(out.schema["properties"]["tags"]["maxItems"], 5);
    }

    #[test]
    fn collects_unsupported_keyword_diagnostics() {
        let fixture = json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "pattern": "^[a-z]+$",
                    "minLength": 3,
                    "allOf": [{ "type": "string" }]
                }
            }
        });
        let out = norm(fixture);
        let messages = out.diagnostics.join("\n");
        for keyword in ["pattern", "minLength", "allOf"] {
            assert!(
                messages.contains(keyword),
                "diagnostic for {keyword} missing: {messages}"
            );
        }
        // Dropped keywords do not reach the model schema.
        let name = &out.schema["properties"]["name"];
        assert!(!name.as_object().unwrap().contains_key("pattern"));
        assert!(!name.as_object().unwrap().contains_key("allOf"));
        assert_eq!(name["type"], "string");
    }

    #[test]
    fn reports_non_local_refs_and_missing_defs() {
        let fixture = json!({
            "type": "object",
            "properties": {
                "bad": { "$ref": "#/components/schemas/X" },
                "missing": { "$ref": "#/$defs/Nope" }
            }
        });
        let out = norm(fixture);
        let messages = out.diagnostics.join("\n");
        assert!(messages.contains("non-local $ref"), "{messages}");
        assert!(messages.contains("unresolvable $ref"), "{messages}");
        assert_eq!(out.schema["properties"]["bad"], json!({}));
    }

    #[test]
    fn empty_object_schema_and_no_properties() {
        for fixture in [
            json!({ "type": "object", "properties": {} }),
            json!({}),
            json!({ "properties": {} }),
        ] {
            let out = norm(fixture);
            assert_eq!(out.schema["type"], "object");
            assert_eq!(out.schema["properties"], json!({}));
            assert_eq!(out.schema["additionalProperties"], false);
        }
    }

    #[test]
    fn preserves_declared_additional_properties_schema() {
        let fixture = json!({
            "type": "object",
            "properties": { "known": { "type": "string" } },
            "additionalProperties": { "type": "integer" }
        });
        let out = norm(fixture);
        assert_eq!(
            out.schema["additionalProperties"],
            json!({ "type": "integer" })
        );
        assert!(out.diagnostics.is_empty(), "{:?}", out.diagnostics);
    }

    #[test]
    fn array_items_are_recursively_normalized() {
        let fixture = json!({
            "type": "object",
            "properties": {
                "matrix": {
                    "type": "array",
                    "items": {
                        "type": "array",
                        "items": { "type": "integer" }
                    }
                }
            }
        });
        let out = norm(fixture);
        assert!(out.diagnostics.is_empty(), "{:?}", out.diagnostics);
        assert_eq!(out.schema["properties"]["matrix"]["type"], "array");
        let items = &out.schema["properties"]["matrix"]["items"];
        assert_eq!(items["type"], "array");
        assert_eq!(items["items"]["type"], "integer");
    }

    #[test]
    fn multi_type_arrays_are_diagnosed() {
        let fixture = json!({
            "type": "object",
            "properties": { "x": { "type": ["string", "integer"] } }
        });
        let out = norm(fixture);
        assert!(
            out.diagnostics.join("\n").contains("unsupported"),
            "{:?}",
            out.diagnostics
        );
        assert_eq!(out.schema["properties"]["x"]["type"], "string");
    }

    // -- validator: fixtures ----------------------------------------------

    #[test]
    fn validator_checks_required_and_types() {
        let schema = json!({
            "type": "object",
            "properties": {
                "path": { "type": "string" },
                "count": { "type": "integer" },
                "flag": { "type": "boolean" },
                "maybe": { "type": ["string", "null"] }
            },
            "required": ["path"],
            "additionalProperties": false
        });
        validate_ok(
            json!({ "path": "a.txt", "count": 3, "flag": true, "maybe": null }),
            &schema,
        );
        validate_ok(json!({ "path": "a.txt" }), &schema);
        validate_err(json!({}), &schema, &["missing required property \"path\""]);
        validate_err(
            json!({ "path": 42 }),
            &schema,
            &["expected \"string\", got number 42"],
        );
        validate_err(
            json!({ "path": "a", "count": 1.5 }),
            &schema,
            &["expected \"integer\", got number 1.5"],
        );
        validate_err(
            json!({ "path": "a", "extra": 1 }),
            &schema,
            &["unexpected property \"extra\""],
        );
    }

    #[test]
    fn validator_checks_nested_objects_and_arrays() {
        let schema = json!({
            "type": "object",
            "properties": {
                "edits": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "oldText": { "type": "string" },
                            "newText": { "type": "string" }
                        },
                        "required": ["oldText", "newText"],
                        "additionalProperties": false
                    },
                    "minItems": 1,
                    "maxItems": 3
                }
            },
            "required": ["edits"],
            "additionalProperties": false
        });
        validate_ok(
            json!({ "edits": [{ "oldText": "a", "newText": "b" }, { "oldText": "b", "newText": "c" }] }),
            &schema,
        );
        validate_err(
            json!({ "edits": [] }),
            &schema,
            &["expected at least 1 items"],
        );
        validate_err(
            json!({ "edits": [{ "oldText": "a" }] }),
            &schema,
            &["missing required property \"newText\""],
        );
        validate_err(
            json!({ "edits": [{ "oldText": "a", "newText": "b", "surprise": true }] }),
            &schema,
            &["unexpected property \"surprise\""],
        );
        validate_err(
            json!({ "edits": [{ "oldText": 1, "newText": "b" }] }),
            &schema,
            &["oldText: expected \"string\""],
        );
    }

    #[test]
    fn validator_checks_enum_and_numeric_bounds() {
        let schema = json!({
            "type": "object",
            "properties": {
                "sort": { "type": "string", "enum": ["name", "size"] },
                "limit": { "type": "integer", "minimum": 1, "maximum": 999999 },
                "index": { "type": "integer", "minimum": 0 }
            },
            "required": ["sort"],
            "additionalProperties": false
        });
        validate_ok(json!({ "sort": "size", "limit": 5, "index": 0 }), &schema);
        validate_err(json!({ "sort": "mtime" }), &schema, &["not one of"]);
        validate_err(
            json!({ "sort": "name", "limit": 0 }),
            &schema,
            &["below minimum 1"],
        );
        validate_err(
            json!({ "sort": "name", "limit": 1000000 }),
            &schema,
            &["above maximum 999999"],
        );
        validate_err(
            json!({ "sort": "name", "index": -1 }),
            &schema,
            &["below minimum 0"],
        );
    }

    #[test]
    fn validator_union_branches_and_additional_property_schema() {
        let schema = json!({
            "type": "object",
            "properties": { "known": { "type": "string" } },
            "additionalProperties": { "type": "integer" }
        });
        validate_ok(json!({ "known": "a", "extra": 7 }), &schema);
        validate_err(
            json!({ "known": "a", "extra": "no" }),
            &schema,
            &["expected \"integer\""],
        );

        let union = json!({
            "type": "object",
            "properties": { "v": { "anyOf": [{ "type": "string" }, { "type": "integer" }] } },
            "additionalProperties": false
        });
        validate_ok(json!({ "v": "x" }), &union);
        validate_ok(json!({ "v": 3 }), &union);
        validate_err(
            json!({ "v": true }),
            &union,
            &["does not match any union branch"],
        );
    }

    #[test]
    fn validator_accepts_unconstrained_nodes() {
        let schema =
            json!({ "type": "object", "properties": { "x": {} }, "additionalProperties": false });
        validate_ok(json!({ "x": { "anything": [1, 2, "three"] } }), &schema);
        let no_type = json!({ "type": "object", "properties": { "y": { "description": "no type" } }, "additionalProperties": false });
        validate_ok(json!({ "y": null }), &no_type);
        validate_ok(json!({ "y": "anything" }), &no_type);
    }
}