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).
This commit is contained in:
parent
bbd616b3a4
commit
b391db6a74
@ -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 `<xs:sequence minOccurs="2">` (2 > the default 1) and `<xs:all maxOccurs="0">` (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`.
|
||||
|
||||
@ -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<String>? {
|
||||
|
||||
@ -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<Substring> = 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 {
|
||||
|
||||
@ -29,6 +29,41 @@ struct SchemaAllGroupPlacementTests {
|
||||
+ "<xs:group ref=\"g\"/></xs:sequence></xs:complexType>"))
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
"<xs:complexType name=\"T\">\(inner)</xs:complexType>"
|
||||
}
|
||||
// minOccurs="2" with an absent (default 1) maxOccurs is 2 > 1.
|
||||
#expect(!compiles(ct("<xs:sequence minOccurs=\"2\"><xs:element name=\"e\"/></xs:sequence>")))
|
||||
#expect(!compiles(ct("<xs:choice minOccurs=\"2\"><xs:element name=\"e\"/></xs:choice>")))
|
||||
#expect(!compiles(ct("<xs:sequence minOccurs=\"999999999\"><xs:element name=\"e\"/></xs:sequence>")))
|
||||
// An explicit, well-ordered range stays valid; unbounded is never exceeded.
|
||||
#expect(compiles(ct("<xs:sequence minOccurs=\"2\" maxOccurs=\"4\"><xs:element name=\"e\"/></xs:sequence>")))
|
||||
#expect(compiles(ct("<xs:choice minOccurs=\"2\" maxOccurs=\"unbounded\"><xs:element name=\"e\"/></xs:choice>")))
|
||||
#expect(compiles(ct("<xs:sequence><xs:element name=\"e\" minOccurs=\"0\"/></xs:sequence>")))
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
"<xs:complexType name=\"T\">\(inner)</xs:complexType>"
|
||||
}
|
||||
#expect(!compiles(ct("<xs:all minOccurs=\"2\"><xs:element name=\"e\"/></xs:all>")))
|
||||
#expect(!compiles(ct("<xs:all maxOccurs=\"0\"><xs:element name=\"e\"/></xs:all>")))
|
||||
#expect(!compiles(ct("<xs:all minOccurs=\"0\" maxOccurs=\"0\"><xs:element name=\"e\"/></xs:all>")))
|
||||
#expect(!compiles(ct("<xs:all maxOccurs=\"unbounded\"><xs:element name=\"e\"/></xs:all>")))
|
||||
#expect(!compiles(ct("<xs:all><xs:element name=\"e\" maxOccurs=\"2\"/></xs:all>")))
|
||||
// minOccurs 0 or 1 with the default maxOccurs of 1 is valid.
|
||||
#expect(compiles(ct("<xs:all minOccurs=\"0\"><xs:element name=\"e\"/></xs:all>")))
|
||||
#expect(compiles(ct("<xs:all><xs:element name=\"e\" minOccurs=\"0\"/></xs:all>")))
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user