From b391db6a7413bb177587e56249c6f3403c630fbd Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 17 Jun 2026 20:55:22 +0200 Subject: [PATCH] fix(schema): enforce model-group occurrence ranges; invalid-schemas-accepted 130 to 110 (#145) Enforce the two occurrence-range constraints on model groups that PureXML was not checking: - p-props-correct.1: a particle's minOccurs may not exceed its maxOccurs, both defaulting to 1 when the attribute is absent. occurrenceOrderErrors previously fired only when BOTH attributes were present, so it missed minOccurs="2" with a defaulted maxOccurs (2 > 1) and maxOccurs="0" with a defaulted minOccurs (1 > 0). It now orders against the effective defaults. - cos-all-limited.2: an all group's maxOccurs must be 1 and its minOccurs 0 or 1, and every particle it contains is limited to maxOccurs 0 or 1. Both are zero-false-positive rules (no valid schema violates them). XSTS: invalid-schemas-accepted 130 to 110 (20 caught), valid-schemas-rejected held at 0, no other bucket moved. The differential harness corroborates: agree-invalid 1751 to 1767, PureXML-stricter unchanged at 87 (no new over-rejection across 9618 schemas). --- CHANGELOG.md | 4 ++ Sources/Schema/SchemaStructure.swift | 39 ++++++++++++-------- Sources/Schema/SchemaStructureHelpers.swift | 41 ++++++++++++++++++--- Tests/SchemaAllGroupPlacementTests.swift | 35 ++++++++++++++++++ Tests/XSTSSuiteTests.swift | 6 ++- 5 files changed, 102 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c470e49..613e5d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Schema compile diagnostics are now located: `SchemaError.inconsistent` carries `[ValidationError]` with coding paths that resolve to `TreeNode.sourceRange` on a ranged schema tree (#169). Parse-time facet and structure findings attach to their declaring nodes where possible; `SchemaError.inconsistentFindings` exposes the full list. Schematron reuses the shared `validationCodingPath()` helper. - DTD parse-time validity findings are warnings until strict validation promotes them; `validateAgainstInternalDTD` returns full findings (errors and warnings). + +### Fixed + +- XSTS invalid-schemas-accepted (#145): the occurrence-range constraints on model groups are enforced. A particle's `minOccurs` may not exceed its `maxOccurs`, both defaulting to 1 when absent (`p-props-correct.1`), so `` (2 > the default 1) and `` (the default 1 > 0) are rejected; and an `all` group's `maxOccurs` must be 1 and `minOccurs` 0 or 1, with every particle it contains limited to `maxOccurs` 0 or 1 (`cos-all-limited.2`). The previous check fired only when both attributes were present, missing the common default-`maxOccurs` cases. Triple-confirmed against the W3C-labelled suite and libxml2: invalid-schemas-accepted 130 → 110, with no false positive (valid-schemas-rejected held at 0) and no other bucket moved. - 2026-06-16 Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-schemas-accepted 394 → 266** (settled expectations), **valid-instances-rejected 180 → 171**, **invalid-instances-accepted 158 → 155**, **valid-schemas-rejected held at 1** (`particlesZ001`, spec-ambiguous). Not yet 1.0: see `docs/production-readiness.md`. diff --git a/Sources/Schema/SchemaStructure.swift b/Sources/Schema/SchemaStructure.swift index 8852be4..174c5f1 100644 --- a/Sources/Schema/SchemaStructure.swift +++ b/Sources/Schema/SchemaStructure.swift @@ -143,30 +143,37 @@ extension PureXML.Schema.XSDParser { } private static func structuralErrors(_ node: XSDTree, local: String, names: [String]) -> [String] { - var errors: [String] = [] switch local { case "any", "anyAttribute": - errors += wildcardNamespaceErrors(node) + wildcardNamespaceErrors(node) case "restriction": - errors += restrictionErrors(node, names: names) + restrictionErrors(node, names: names) case "list": - errors += listErrors(node, names: names) + listErrors(node, names: names) case "schema": - errors += schemaChildrenOrderErrors(names) + schemaChildrenOrderErrors(names) case "import": - errors += importErrors(node) - case "element": - errors += elementChildrenErrors(node, names: names) - case "attribute": - errors += attributeChildrenErrors(node, names: names) - case "attributeGroup": - errors += attributeGroupChildrenErrors(node, names: names) - case "group": - errors += groupChildrenErrors(node, names: names) + importErrors(node) default: - break + containerStructuralErrors(node, local: local, names: names) + } + } + + private static func containerStructuralErrors(_ node: XSDTree, local: String, names: [String]) -> [String] { + switch local { + case "element": + elementChildrenErrors(node, names: names) + case "attribute": + attributeChildrenErrors(node, names: names) + case "attributeGroup": + attributeGroupChildrenErrors(node, names: names) + case "group": + groupChildrenErrors(node, names: names) + case "all": + allGroupLimitedErrors(node) + default: + [] } - return errors } private static func allowedChildren(for local: String, node: XSDTree) -> Set? { diff --git a/Sources/Schema/SchemaStructureHelpers.swift b/Sources/Schema/SchemaStructureHelpers.swift index 2745276..c4b2fe1 100644 --- a/Sources/Schema/SchemaStructureHelpers.swift +++ b/Sources/Schema/SchemaStructureHelpers.swift @@ -35,16 +35,45 @@ extension PureXML.Schema.XSDParser { return !digits.isEmpty && digits.allSatisfy { $0.isASCII && $0.isNumber } } + /// p-props-correct.1 (XSD 1.0 Structures 3.9.6): a particle's {min occurs} may + /// not exceed its {max occurs}. Both default to 1 when the attribute is absent, + /// so `minOccurs="2"` with no `maxOccurs` means 2 > 1 and is invalid, as is + /// `maxOccurs="0"` with no `minOccurs` (1 > 0). `unbounded` is infinite and never + /// exceeded; a non-integer lexical is a separate, already-reported error. static func occurrenceOrderErrors(_ node: XSDTree) -> [String] { - guard let minRaw = PureXML.Schema.XSDNode.attribute(node, "minOccurs"), - let maxRaw = PureXML.Schema.XSDNode.attribute(node, "maxOccurs") - else { return [] } - let minimum = minRaw.trimmingXMLWhitespace() - let maximum = maxRaw.trimmingXMLWhitespace() + let minRaw = PureXML.Schema.XSDNode.attribute(node, "minOccurs") + let maxRaw = PureXML.Schema.XSDNode.attribute(node, "maxOccurs") + guard minRaw != nil || maxRaw != nil else { return [] } + let minimum = (minRaw ?? "1").trimmingXMLWhitespace() + let maximum = (maxRaw ?? "1").trimmingXMLWhitespace() guard maximum != "unbounded", isNonNegativeInteger(minimum), isNonNegativeInteger(maximum), exceeds(minimum, maximum) else { return [] } - return ["minOccurs (\(minRaw)) exceeds maxOccurs (\(maxRaw))"] + return ["minOccurs (\(minRaw ?? "1")) exceeds maxOccurs (\(maxRaw ?? "1"))"] + } + + /// cos-all-limited clause 2 (XSD 1.0 Structures 3.8.6): the {max occurs} of an + /// `all` group must be 1 and its {min occurs} 0 or 1, and the {max occurs} of + /// every particle the group contains must be 0 or 1. `unbounded`, or any value + /// above 1, is invalid. (The lexical validity of the values is checked + /// elsewhere; this orders only well-formed non-negative integers.) + static func allGroupLimitedErrors(_ node: XSDTree) -> [String] { + var errors: [String] = [] + func limited(_ value: String?, allowZero: Bool, what: String) { + guard let raw = value?.trimmingXMLWhitespace() else { return } + let allowed: Set = allowZero ? ["0", "1"] : ["1"] + if raw == "unbounded" || (isNonNegativeInteger(raw) && !allowed.contains(canonicalMagnitude(raw))) { + errors.append("\(what) must be \(allowZero ? "0 or 1" : "1")") + } + } + limited(PureXML.Schema.XSDNode.attribute(node, "maxOccurs"), allowZero: false, what: "the maxOccurs of an all group") + limited(PureXML.Schema.XSDNode.attribute(node, "minOccurs"), allowZero: true, what: "the minOccurs of an all group") + let xsd = PureXML.Schema.XSDParser.xsdNamespace + for child in PureXML.Schema.XSDNode.elementChildren(node) { + guard child.name?.namespaceURI == xsd, PureXML.Schema.XSDNode.localName(child) == "element" else { continue } + limited(PureXML.Schema.XSDNode.attribute(child, "maxOccurs"), allowZero: true, what: "the maxOccurs of a particle in an all group") + } + return errors } static func exceeds(_ lhs: String, _ rhs: String) -> Bool { diff --git a/Tests/SchemaAllGroupPlacementTests.swift b/Tests/SchemaAllGroupPlacementTests.swift index 732e253..0d041ef 100644 --- a/Tests/SchemaAllGroupPlacementTests.swift +++ b/Tests/SchemaAllGroupPlacementTests.swift @@ -29,6 +29,41 @@ struct SchemaAllGroupPlacementTests { + "")) } + /// p-props-correct.1: minOccurs may not exceed maxOccurs, both defaulting to 1. + /// A model group with `minOccurs="2"` and no `maxOccurs` means 2 > 1 and is + /// invalid; `maxOccurs="0"` with no `minOccurs` means 1 > 0 and is invalid. + @Test("minOccurs may not exceed maxOccurs on a model group") + func test_occurrenceRangeOnModelGroups() { + func ct(_ inner: String) -> String { + "\(inner)" + } + // minOccurs="2" with an absent (default 1) maxOccurs is 2 > 1. + #expect(!compiles(ct(""))) + #expect(!compiles(ct(""))) + #expect(!compiles(ct(""))) + // An explicit, well-ordered range stays valid; unbounded is never exceeded. + #expect(compiles(ct(""))) + #expect(compiles(ct(""))) + #expect(compiles(ct(""))) + } + + /// cos-all-limited clause 2: an all group's maxOccurs must be 1 and its minOccurs + /// 0 or 1, and every particle it contains has maxOccurs 0 or 1. + @Test("an all group's occurrence is limited to 0/1") + func test_allGroupOccurrenceLimited() { + func ct(_ inner: String) -> String { + "\(inner)" + } + #expect(!compiles(ct(""))) + #expect(!compiles(ct(""))) + #expect(!compiles(ct(""))) + #expect(!compiles(ct(""))) + #expect(!compiles(ct(""))) + // minOccurs 0 or 1 with the default maxOccurs of 1 is valid. + #expect(compiles(ct(""))) + #expect(compiles(ct(""))) + } + /// An imported namespace may define an all-group whose local name collides with /// a *non-all* group in this schema's own target namespace. A reference to the /// local group inside a compositor resolves to this target namespace and is diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 3e78367..28bafa4 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -141,8 +141,12 @@ struct XSTSSuiteTests { /// gap) is accepted, 132 → 133. Per the production standard over-rejection is the /// non-starter and under-rejection is recoverable; this debt is recovered by the /// full effective-total-range Particle-Valid-Restriction (tracked). + /// Enforcing the occurrence-range constraints on model groups (p-props-correct.1: + /// minOccurs may not exceed maxOccurs, both defaulting to 1; cos-all-limited.2: an + /// all group's maxOccurs is 1 and minOccurs 0 or 1, its particles' maxOccurs 0 or + /// 1) then caught twenty more invalid schemas with no false positive: 130 → 110. private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 130 + private let knownSchemaInvalidAccepted = 110 private let knownInstanceValidRejected = 0 private let knownInstanceInvalidAccepted = 133