From dd10f32abb917518441b3bfe9c212a8311619bdc Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sun, 14 Jun 2026 14:30:25 +0200 Subject: [PATCH] feat(schema): a simpleType must have exactly one of restriction/list/union Per XSD 1.0 Datatypes a simpleType's content is (annotation?, (restriction | list | union)). The membership table admitted each name without the cardinality, so an empty simpleType or one combining two derivations (two unions, a union and a list, two restrictions) was accepted. The exactly-one rule is now enforced, mirroring the named-group compositor check. XSTS: invalid schemas accepted 537 to 527, valid schemas rejected held at 72, instance buckets unchanged. --- CHANGELOG.md | 1 + Sources/Schema/SchemaContentOrder.swift | 10 ++++++++++ Sources/Schema/SchemaStructure.swift | 3 +++ Tests/SchemaStructureTests.swift | 11 +++++++++++ Tests/XSTSSuiteTests.swift | 2 +- docs/schema-validity-burndown.md | 2 +- 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8ade95..d09b009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- A `simpleType` must contain exactly one of `restriction`, `list`, or `union` (#158). Per XSD 1.0 Datatypes its content is `(annotation?, (restriction | list | union))`, but the membership table admitted each name without the cardinality, so an empty `simpleType` or one combining two derivations (two `union`s, a `union` and a `list`, two `restriction`s) was accepted (the stB family). The exactly-one rule is now enforced. XSTS: invalid schemas accepted 537 to 527, no other bucket raised (valid schemas rejected held at 72). - The `final` and `block` derivation-control attributes are now checked against their value space (#158). Each is `#all` or a whitespace list of method tokens, but the admitted tokens depend on the component: an element's `final` is `extension`/`restriction` and its `block` adds `substitution`; a complexType's `final`/`block` are `extension`/`restriction`; a simpleType's `final` is `list`/`union`/`restriction`. The tokens were parsed leniently (unknown tokens ignored, `substitution` admitted everywhere), so `final="substitution"` on an element, a misspelled or miscased token, or `#all` mixed with other tokens was accepted (the elemF/st_final families). The list is now validated against the component's exact set; `finalDefault`/`blockDefault` (the wider schema-level value space) and foreign attributes are left alone. XSTS: invalid schemas accepted 580 to 537, no other bucket raised (valid schemas rejected held at 72). - A `simpleContent` derivation's content is now checked for order and disallowed children (#158). A `restriction` is `(annotation?, (simpleType?, facet*), (attribute | attributeGroup)*, anyAttribute?)` and an `extension` is `(annotation?, (attribute | attributeGroup)*, anyAttribute?)`: simpleContent has no model group, an extension has no facets or `simpleType`, and the attributes precede a single trailing `anyAttribute`. The membership table admitted the names but not the model, so a model group inside simpleContent, a facet in an extension, a facet before its `simpleType`, an attribute after `anyAttribute`, or a second `anyAttribute` (the ctD family) was accepted. The derivation's children are matched against the context's ordered slot model, reusing the slot walk built for complexContent. XSTS: invalid schemas accepted 594 to 580, no other bucket raised (valid schemas rejected held at 72). - Particle Valid (Restriction) now checks an element's type by derivation, not just its name (NameAndTypeOK, #158). When a restriction maps a same-named element onto a base element, the restricting type must be, or derive by restriction from, the base element's type; renaming `xs:string` to `xs:int`, widening a concrete type to `xs:anyType`, or naming an unrelated type (a different user type, a wider ancestor, an absent/`anyType` type over a concrete base) was accepted because only the names were compared. Each element particle now carries its type's derivation identity (the resolved type name, `anyType` for an absent type, or nil for an inline anonymous type), named `simpleType` restriction derivations are recorded alongside complex ones, and the check walks the user restriction chain and then the built-in lattice to decide derivation by name; an ur-type base, a union base (membership is not a restriction chain), and an inline anonymous type are permitted, so no valid restriction is rejected. XSTS: invalid schemas accepted 621 to 594, invalid instances accepted 165 to 160 (more invalid schemas rejected up front), no bucket raised (valid schemas rejected held at 72). diff --git a/Sources/Schema/SchemaContentOrder.swift b/Sources/Schema/SchemaContentOrder.swift index a068c72..290d632 100644 --- a/Sources/Schema/SchemaContentOrder.swift +++ b/Sources/Schema/SchemaContentOrder.swift @@ -1,4 +1,14 @@ extension PureXML.Schema.XSDParser { + /// The finding, if any, for a `simpleType`: its content is exactly one of + /// `restriction`/`list`/`union` (XSD 1.0 Datatypes), so neither an empty + /// `simpleType` nor one combining two derivations (two unions, a union and a + /// list, ...) is valid. The `allowedChildren` membership check admits each name + /// but not the cardinality. + static func simpleTypeContentErrors(_ children: [String]) -> [String] { + let derivations = children.count { $0 == "restriction" || $0 == "list" || $0 == "union" } + return derivations == 1 ? [] : ["a simpleType must contain exactly one of restriction, list, or union"] + } + /// Content-model order and cardinality for a `complexContent` derivation, the /// ordering complement to the set-membership `allowedChildren` check (which /// admits the right child names but not their sequence or count). diff --git a/Sources/Schema/SchemaStructure.swift b/Sources/Schema/SchemaStructure.swift index 6b9f937..6f8414b 100644 --- a/Sources/Schema/SchemaStructure.swift +++ b/Sources/Schema/SchemaStructure.swift @@ -290,6 +290,9 @@ extension PureXML.Schema.XSDParser { if local == "complexType" { errors += complexTypeContentErrors(children) } + if local == "simpleType" { + errors += simpleTypeContentErrors(children) + } return errors } diff --git a/Tests/SchemaStructureTests.swift b/Tests/SchemaStructureTests.swift index 3a29001..f47f5c2 100644 --- a/Tests/SchemaStructureTests.swift +++ b/Tests/SchemaStructureTests.swift @@ -288,4 +288,15 @@ struct SchemaStructureTests { """#)) } + + @Test("a simpleType must contain exactly one of restriction, list, or union") + func test_simpleTypeContent() { + // Two derivations, or a mix, is invalid (stB). + #expect(rejects(#""#)) + #expect(rejects(#""#)) + #expect(rejects(#""#)) + // Exactly one compiles. + #expect(!rejects(#""#)) + #expect(!rejects(#""#)) + } } diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 8c0fe64..55a708a 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -29,7 +29,7 @@ struct XSTSSuiteTests { /// XSTS` (debug is far slower and the corpus is large). Per-case deviations /// are written to /tmp/xsts-failures.txt for the burn-down. private let knownSchemaValidRejected = 72 - private let knownSchemaInvalidAccepted = 537 + private let knownSchemaInvalidAccepted = 527 private let knownInstanceValidRejected = 214 private let knownInstanceInvalidAccepted = 160 diff --git a/docs/schema-validity-burndown.md b/docs/schema-validity-burndown.md index 477c0ae..462a939 100644 --- a/docs/schema-validity-burndown.md +++ b/docs/schema-validity-burndown.md @@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open. | Bucket | Count | Reading | |---|---|---| | valid-schemas-rejected | 72 | we rarely reject a good schema | -| **invalid-schemas-accepted** | **2461 -> 537** | we rarely catch a bad one (facet definition + applicability, default/fixed value validity, particle-restriction name+type, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) | +| **invalid-schemas-accepted** | **2461 -> 527** | we rarely catch a bad one (facet definition + applicability, default/fixed value validity, particle-restriction name+type, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) | | valid-instances-rejected | 233 | (instance side, separately tracked) | | invalid-instances-accepted | 165 | |