xmlschema 0.0.5

XML Schema (XSD) validation for Rust, with zero unsafe code
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 xmlschema. All rights reserved.

//! Validation behaviour against real schemas.

use xmlschema::{Report, parse_schema, validate};

const LIBRARY_XSD: &str = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="isbn">
    <xs:restriction base="xs:string">
      <xs:pattern value="\d{3}-\d{10}"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="lang">
    <xs:restriction base="xs:string">
      <xs:enumeration value="en"/>
      <xs:enumeration value="fr"/>
      <xs:enumeration value="de"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string"/>
              <xs:element name="year">
                <xs:simpleType>
                  <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1450"/>
                    <xs:maxInclusive value="2100"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="lang" type="lang" use="required"/>
            <xs:attribute name="isbn" type="isbn"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
"#;

fn check(xml: &str) -> Report {
    let schema = parse_schema(LIBRARY_XSD).expect("schema parses");
    let doc = oxml::parse(xml).expect("document parses");
    validate(&doc, &schema)
}

#[test]
fn a_conforming_document_is_valid() {
    let report = check(
        r#"<library>
             <book lang="en" isbn="978-0441013593">
               <title>Dune</title><year>1965</year>
             </book>
           </library>"#,
    );
    assert!(report.is_valid(), "{report}");
}

#[test]
fn repeated_elements_within_bounds_are_valid() {
    let report = check(
        r#"<library>
             <book lang="en"><title>A</title><year>2000</year></book>
             <book lang="fr"><title>B</title><year>2001</year></book>
             <book lang="de"><title>C</title><year>2002</year></book>
           </library>"#,
    );
    assert!(report.is_valid(), "{report}");
}

#[test]
fn a_missing_required_attribute_is_reported_with_its_path() {
    let report = check(
        r"<library><book><title>A</title><year>2000</year></book></library>",
    );
    assert_eq!(report.violations.len(), 1);
    assert_eq!(report.violations[0].path, "/library/book[1]");
    assert!(
        report.violations[0].message.contains("lang"),
        "{}",
        report.violations[0].message
    );
}

#[test]
fn an_enumeration_violation_names_the_permitted_values() {
    let report = check(
        r#"<library><book lang="es"><title>A</title><year>2000</year></book></library>"#,
    );
    assert_eq!(report.violations.len(), 1);
    assert_eq!(report.violations[0].path, "/library/book[1]/@lang");
    let m = &report.violations[0].message;
    assert!(m.contains("en") && m.contains("fr"), "{m}");
}

#[test]
fn a_pattern_violation_is_reported() {
    let report = check(
        r#"<library><book lang="en" isbn="nope">
             <title>A</title><year>2000</year></book></library>"#,
    );
    assert_eq!(report.violations.len(), 1);
    assert!(
        report.violations[0].message.contains("pattern"),
        "{}",
        report.violations[0].message
    );
}

#[test]
fn numeric_bounds_are_enforced() {
    let too_old = check(
        r#"<library><book lang="en"><title>A</title><year>1200</year></book></library>"#,
    );
    assert_eq!(too_old.violations.len(), 1);
    assert!(too_old.violations[0].message.contains("1450"));

    let ok = check(
        r#"<library><book lang="en"><title>A</title><year>1450</year></book></library>"#,
    );
    assert!(ok.is_valid(), "{ok}");
}

#[test]
fn a_non_integer_where_an_integer_is_required_is_reported() {
    let report = check(
        r#"<library><book lang="en"><title>A</title><year>MCMLXV</year></book></library>"#,
    );
    assert_eq!(report.violations.len(), 1);
    assert!(
        report.violations[0].message.contains("integer"),
        "{}",
        report.violations[0].message
    );
}

#[test]
fn missing_children_report_the_expected_cardinality() {
    let report = check(r#"<library><book lang="en"></book></library>"#);
    let messages: Vec<&str> = report
        .violations
        .iter()
        .map(|v| v.message.as_str())
        .collect();
    assert!(messages.iter().any(|m| m.contains("title")), "{messages:?}");
    assert!(messages.iter().any(|m| m.contains("year")), "{messages:?}");
}

#[test]
fn an_unexpected_element_names_what_was_allowed() {
    let report = check(
        r#"<library><book lang="en"><title>A</title><year>2000</year>
             <publisher>X</publisher></book></library>"#,
    );
    assert_eq!(report.violations.len(), 1);
    let m = &report.violations[0].message;
    assert!(m.contains("publisher") && m.contains("title"), "{m}");
}

/// Elements out of order are an *ordering* problem, and the message
/// should not blame cardinality for it.
#[test]
fn out_of_order_children_are_reported() {
    let report = check(
        r#"<library><book lang="en"><year>2000</year><title>A</title></book></library>"#,
    );
    assert!(!report.is_valid());
}

#[test]
fn every_violation_is_reported_not_just_the_first() {
    let report = check(
        r#"<library>
             <book lang="es"><title>A</title><year>nope</year></book>
             <book><title>B</title><year>1200</year></book>
           </library>"#,
    );
    // bad lang, bad year, missing lang, out-of-range year.
    assert!(
        report.violations.len() >= 4,
        "expected at least 4, got {}: {report}",
        report.violations.len()
    );
}

#[test]
fn an_unknown_root_element_lists_what_the_schema_declares() {
    let schema = parse_schema(LIBRARY_XSD).expect("schema parses");
    let doc = oxml::parse("<catalogue/>").expect("parses");
    let report = validate(&doc, &schema);
    assert_eq!(report.violations.len(), 1);
    assert!(report.violations[0].message.contains("library"));
}

#[test]
fn a_malformed_schema_is_rejected_with_a_reason() {
    let err = parse_schema("<xs:schema>").expect_err("not well-formed");
    assert!(err.message.contains("well-formed"), "{}", err.message);

    let err = parse_schema("<root/>").expect_err("not a schema");
    assert!(err.message.contains("xs:schema"), "{}", err.message);
}

#[test]
fn choice_accepts_any_declared_branch() {
    let xsd = r#"
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="payment">
          <xs:complexType>
            <xs:choice>
              <xs:element name="card" type="xs:string"/>
              <xs:element name="cash" type="xs:string"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");

    for xml in [
        "<payment><card>x</card></payment>",
        "<payment><cash>y</cash></payment>",
    ] {
        let doc = oxml::parse(xml).expect("parses");
        assert!(validate(&doc, &schema).is_valid(), "{xml}");
    }

    let doc =
        oxml::parse("<payment><cheque>z</cheque></payment>").expect("parses");
    let report = validate(&doc, &schema);
    assert_eq!(report.violations.len(), 1);
    assert!(report.violations[0].message.contains("card"));
}

/// `xs:any` matches by namespace, and `processContents` decides how
/// hard the match is validated.
#[test]
fn a_wildcard_matches_by_namespace() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="known" type="xs:integer"/>
  <xs:element name="r">
    <xs:complexType><xs:sequence>
      <xs:element name="a" type="xs:string"/>
      <xs:any namespace='##any' processContents="lax" maxOccurs="unbounded"/>
    </xs:sequence></xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    // The wildcard admits an element the type never declared.
    assert!(valid("<r><a>x</a><anything/></r>"));
    assert!(valid("<r><a>x</a><one/><two/></r>"));
    // Under `lax` a matched element *with* a declaration is still
    // validated against it.
    assert!(valid("<r><a>x</a><known>42</known></r>"));
    assert!(
        !valid("<r><a>x</a><known>not a number</known></r>"),
        "lax still validates what it can resolve"
    );
    // The wildcard does not excuse the declared particle.
    assert!(!valid("<r><anything/></r>"), "`a` is still required");
}

/// A strict wildcard requires the matched element to be declared.
#[test]
fn a_strict_wildcard_requires_a_declaration() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="known" type="xs:string"/>
  <xs:element name="r">
    <xs:complexType><xs:sequence>
      <xs:any namespace='##any' processContents="strict"/>
    </xs:sequence></xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    assert!(valid("<r><known>x</known></r>"));
    assert!(!valid("<r><undeclared/></r>"), "strict needs a declaration");
}

/// `xs:anyAttribute` admits attributes the type does not declare.
#[test]
fn an_attribute_wildcard_admits_undeclared_attributes() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:complexType>
      <xs:attribute name="known" type="xs:integer"/>
      <xs:anyAttribute namespace='##any' processContents="lax"/>
    </xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    assert!(valid(r#"<r other="anything"/>"#));
    // The declared one is still checked.
    assert!(!valid(r#"<r known="not a number"/>"#));
}

/// `xs:all` reports each way a document can break it.
#[test]
fn the_all_group_reports_what_went_wrong() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:complexType><xs:all>
      <xs:element name="a" type="xs:string"/>
      <xs:element name="b" type="xs:integer" minOccurs="0"/>
    </xs:all></xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let report = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema)
    };
    assert!(report("<r><a>x</a></r>").is_valid(), "b is optional");
    assert!(report("<r><b>1</b><a>x</a></r>").is_valid(), "any order");

    // A repeat, an undeclared child, a missing required child, and a
    // child whose own type is violated: each is reported.
    for (xml, expect) in [
        ("<r><a>x</a><a>y</a></r>", "more than once"),
        ("<r><a>x</a><c/></r>", "not permitted"),
        ("<r><b>1</b></r>", "missing required"),
        ("<r><a>x</a><b>no</b></r>", "integer"),
    ] {
        let r = report(xml);
        assert!(!r.is_valid(), "{xml} should be invalid");
        assert!(
            r.violations.iter().any(|v| v.message.contains(expect)),
            "{xml}: expected a message about `{expect}`, got {:?}",
            r.violations
        );
    }
}

/// A union reports that nothing matched, and a list says which item.
#[test]
fn simple_type_varieties_report_usefully() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="u">
    <xs:simpleType><xs:union memberTypes="xs:integer xs:date"/></xs:simpleType>
  </xs:element>
  <xs:element name="l">
    <xs:simpleType>
      <xs:restriction>
        <xs:simpleType><xs:list itemType="xs:integer"/></xs:simpleType>
        <xs:length value="2"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let report = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema)
    };

    let r = report("<u>neither</u>");
    assert!(!r.is_valid());
    assert!(
        r.violations[0].message.contains("member types"),
        "{:?}",
        r.violations
    );

    let r = report("<l>1 two</l>");
    assert!(!r.is_valid());
    assert!(
        r.violations[0].message.contains("list item"),
        "{:?}",
        r.violations
    );

    // Length counts items, and says so.
    let r = report("<l>1 2 3</l>");
    assert!(!r.is_valid());
    assert!(
        r.violations[0].message.contains("items"),
        "{:?}",
        r.violations
    );
    assert!(report("<l>1 2</l>").is_valid());
}

/// A document whose root has no declaration cannot be validated.
#[test]
fn an_undeclared_root_is_reported_rather_than_ignored() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="known" type="xs:string"/>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let doc = oxml::parse("<unknown/>").expect("well-formed");
    let report = validate(&doc, &schema);
    assert!(!report.is_valid());
    assert!(!report.violations.is_empty());
}

/// A violation carries where it happened, not only what.
#[test]
fn a_violation_carries_its_path() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:complexType><xs:sequence>
      <xs:element name="n" type="xs:integer" maxOccurs="unbounded"/>
    </xs:sequence></xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let doc = oxml::parse("<r><n>1</n><n>bad</n></r>").expect("well-formed");
    let report = validate(&doc, &schema);
    assert!(!report.is_valid());
    // The second child, identified positionally.
    assert!(
        report.violations[0].path.contains("[2]"),
        "path should locate the second `n`: {:?}",
        report.violations
    );
    // And the report renders.
    assert!(!report.to_string().is_empty());
}

/// A report renders every violation it holds.
#[test]
fn a_report_renders_its_violations() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:complexType><xs:sequence>
      <xs:element name="a" type="xs:integer"/>
      <xs:element name="b" type="xs:integer"/>
    </xs:sequence></xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let doc = oxml::parse("<r><a>x</a><b>y</b></r>").expect("well-formed");
    let report = validate(&doc, &schema);
    assert!(!report.is_valid());
    let text = report.to_string();
    // Both violations appear, each with its path.
    assert!(text.contains("/r/a"), "{text}");
    assert!(text.contains("/r/b"), "{text}");

    // And a clean report says so in one word.
    let ok = oxml::parse("<r><a>1</a><b>2</b></r>").expect("well-formed");
    assert_eq!(validate(&ok, &schema).to_string(), "valid");
}

/// A fixed *attribute* value must be exactly that value.
#[test]
fn a_fixed_attribute_value_is_enforced() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:complexType>
      <xs:attribute name="k" type="xs:string" fixed="only"/>
    </xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    assert!(valid("<r/>"), "fixed does not make it required");
    assert!(valid(r#"<r k="only"/>"#));
    assert!(!valid(r#"<r k="other"/>"#));
}

/// A strict attribute wildcard requires a top-level declaration, and
/// never complains about the schema-instance attributes.
#[test]
fn a_strict_attribute_wildcard_requires_a_declaration() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attribute name="known" type="xs:string"/>
  <xs:element name="r">
    <xs:complexType>
      <xs:attribute name="own" type="xs:string"/>
      <xs:anyAttribute namespace='##any' processContents="strict"/>
    </xs:complexType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    assert!(valid(r#"<r own="x"/>"#), "declared on the type");
    assert!(!valid(r#"<r other="x"/>"#), "no declaration anywhere");
    // `xsi:` attributes belong to the schema-instance namespace and
    // are not this schema's to declare.
    assert!(
        valid(
            r#"<r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="false"/>"#
        ),
        "xsi attributes are defined by the specification"
    );
}

/// A wildcard may name the namespaces it admits explicitly.
#[test]
fn a_wildcard_may_list_its_namespaces() {
    let xsd = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                              targetNamespace="urn:t" xmlns:t="urn:t"
                              elementFormDefault="qualified">
        <xs:element name="r">
          <xs:complexType><xs:sequence>
            <xs:any namespace="urn:a urn:b" processContents="skip"/>
          </xs:sequence></xs:complexType>
        </xs:element></xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |xml: &str| {
        let doc = oxml::parse(xml).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    assert!(valid(r#"<r xmlns="urn:t"><x xmlns="urn:a"/></r>"#));
    assert!(valid(r#"<r xmlns="urn:t"><x xmlns="urn:b"/></r>"#));
    assert!(
        !valid(r#"<r xmlns="urn:t"><x xmlns="urn:c"/></r>"#),
        "urn:c is not on the list"
    );
}

/// An enumeration on a list constrains the whole value.
#[test]
fn an_enumeration_on_a_list_matches_the_whole_value() {
    let xsd = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:simpleType>
      <xs:restriction>
        <xs:simpleType><xs:list itemType="xs:integer"/></xs:simpleType>
        <xs:enumeration value="1 2"/>
        <xs:enumeration value="3 4"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>"#;
    let schema = parse_schema(xsd).expect("schema parses");
    let valid = |v: &str| {
        let doc = oxml::parse(&format!("<r>{v}</r>")).expect("well-formed");
        validate(&doc, &schema).is_valid()
    };
    assert!(valid("1 2"));
    assert!(valid("3 4"));
    assert!(!valid("1 3"), "not one of the permitted lists");
    assert!(!valid("1"), "nor is a prefix of one");
}