From 4551508dd957ebbae5209215732b3903059a66c5 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 17 Jun 2026 16:10:57 +0200 Subject: [PATCH] fix(schema): a redefine must name components that exist in the redefined schema (#145) Once the redefined document is loaded through a schemaLoader, every component a redefine names (complexType, simpleType, group, attributeGroup) must have a same-kind component of the same name in the redefined schema (src-redefine.6 / 7.2.1). A redefinition of a component that does not exist there is rejected; a stand-alone schema (target not loaded) is unaffected. XSTS invalid-schemas-accepted 183 to 182, no other bucket moved. --- CHANGELOG.md | 2 ++ Sources/Schema/SchemaReferences.swift | 34 ++++++++++++++++++++++++++- Tests/XSTSSuiteTests.swift | 6 ++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aacbe12..d3472f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s ### Fixed +- XSTS invalid-schemas-accepted (#145): a `redefine` is now checked against the schema it redefines once that schema is loaded: every component the `redefine` names (a `complexType`, `simpleType`, `group`, or `attributeGroup`) must have a same-kind component of the same name in the redefined schema (`src-redefine.6`/`7.2.1`). A redefinition naming a component that does not exist there is rejected. The check runs only when the redefined document was loaded through a `schemaLoader`, so a stand-alone schema is unaffected. Caught one invalid schema: invalid-schemas-accepted 183 → 182, no other bucket moved. + - XSTS invalid-schemas-accepted (#145): an element or attribute `default`/`fixed` value constraint is now validated against an inline type (an inline `simpleType`, or an inline complex type with `simpleContent`) just as it already was against a named `type` reference, so a non-numeric `default` on a decimal-based inline type is rejected. Validated against the simpleContent base's value space, so only a clear value-space mismatch is flagged. Caught two invalid schemas: invalid-schemas-accepted 185 → 183, no other bucket moved. - XSTS invalid-schemas-accepted (#145): a complex type without its own `final` attribute now inherits the schema's `finalDefault` as its `{final}` property, so `finalDefault="#all"` or `"restriction"`/`"extension"` blocks a `complexContent` restriction or extension derived from it just as an explicit `final` would (an explicit `final=""` overrides the default back to no restriction). Complex types previously honoured only an explicit `final`. Caught four invalid schemas: invalid-schemas-accepted 189 → 185, no other bucket moved. diff --git a/Sources/Schema/SchemaReferences.swift b/Sources/Schema/SchemaReferences.swift index 60b7a0e..195a363 100644 --- a/Sources/Schema/SchemaReferences.swift +++ b/Sources/Schema/SchemaReferences.swift @@ -98,7 +98,39 @@ extension PureXML.Schema.XSDParser { ) collectReferenceErrors(source, in: resolutionContext, inheritedBindings: sourceBindings, into: &errors) } - return xsdErrors + simpleContentErrors + errors + return xsdErrors + simpleContentErrors + errors + redefineComponentExistenceErrors(containers) + } + + /// XSD 1.0 `src-redefine.6.1.1`/`6.2.1`/`7.2.1`: each component a `redefine` + /// names must have a same-kind component of that name in the redefined schema. + /// Runs only past the cross-document skip guard above, so the redefined schema + /// is loaded; a redefinition whose name matches nothing in any non-`redefine` + /// container (the loaded targets and the root) names a component that does not + /// exist and is invalid. + private static func redefineComponentExistenceErrors(_ containers: [XSDTree]) -> [String] { + let kinds = ["complexType", "simpleType", "group", "attributeGroup"] + var defined: [String: Set] = [:] + for container in containers where PureXML.Schema.XSDNode.localName(container) != "redefine" { + for kind in kinds { + for node in PureXML.Schema.XSDNode.children(container, named: kind) { + if let name = PureXML.Schema.XSDNode.attribute(node, "name") { + defined[kind, default: []].insert(name) + } + } + } + } + var errors: [String] = [] + for container in containers where PureXML.Schema.XSDNode.localName(container) == "redefine" { + for kind in kinds { + for node in PureXML.Schema.XSDNode.children(container, named: kind) { + guard let name = PureXML.Schema.XSDNode.attribute(node, "name") else { continue } + if defined[kind]?.contains(name) != true { + errors.append("a redefine of \(kind) '\(name)' has no '\(name)' to redefine in the redefined schema") + } + } + } + } + return errors } /// Whether cross-document schema-validity rules should stand down: an external diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 7632968..29ad342 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -148,8 +148,12 @@ struct XSTSSuiteTests { /// inline simpleType, or an inline complex type with simpleContent) the same way /// a named `type` reference already is, then caught two more invalid schemas: /// invalid-schemas accepted 185 → 183, no other bucket moved. + /// Requiring each component a `redefine` names to exist (same kind, same name) in + /// the redefined schema once that schema is loaded (src-redefine.6/7.2.1) then + /// caught one more invalid schema: invalid-schemas accepted 183 → 182, no other + /// bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 183 + private let knownSchemaInvalidAccepted = 182 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132