PureXML/Sources/Schema/SchemaXSIAttribute.swift
Mihaela Mihaljevic 380160cc2b refactor(validation): decompose XSD instance validation into composable rules
The two monolithic XSD content/identity rules only satisfied the OpenAPIKit
validation idiom at the wrong granularity: no per-constraint removability,
no isolable failing/near-miss tests. Split them into 32 named rules, each
finding tagged at its origin and cached per XSDContext so the walk still
runs once per document (the #101 decision against duplicating XSD's
recursive type resolution still holds). Wired the streaming path's ID/IDREF
and identity checks through the same framework instead of calling the
validators imperatively, and added the unwrapAndLookup combinator the rule
already named as required.

Also includes the mandated repo-wide swiftformat/swiftlint pass.
2026-07-12 14:12:17 +02:00

48 lines
2.8 KiB
Swift

extension PureXML.Schema.XSDParser {
private static let xsiInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"
/// XSD 1.0 Schema Component Constraint "xsi: Not Allowed" (§3.2.6): an attribute
/// declaration's {target namespace} must not be the XSI namespace. A schema may
/// target XSI and declare attributes there as long as they stay unqualified, so
/// they land in no namespace (corpus attKb018, attKc018: valid). A top-level
/// attribute, or a local one made qualified by `form="qualified"` or
/// `attributeFormDefault="qualified"`, lands in the target namespace; when that is
/// XSI it is forbidden (corpus attKb018a: `attributeFormDefault="qualified"` puts
/// its attribute-group attributes into XSI). The check is scoped to schemas whose
/// target namespace is XSI, the only documents that can place an attribute there.
static func xsiNamespaceAttributeFindings(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [PureXML.Schema.SchemaLocatedFinding] {
guard context.targetNamespace == xsiInstanceNamespace else { return [] }
let attributeFormDefault = PureXML.Schema.XSDNode.attribute(schema, "attributeFormDefault")
var findings: [PureXML.Schema.SchemaLocatedFinding] = []
collectXSIAttributeFindings(schema, parentIsSchema: false, attributeFormDefault: attributeFormDefault, into: &findings)
return findings
}
private static func collectXSIAttributeFindings(
_ node: XSDTree,
parentIsSchema: Bool,
attributeFormDefault: String?,
into findings: inout [PureXML.Schema.SchemaLocatedFinding],
) {
let local = PureXML.Schema.XSDNode.localName(node)
if local == "appinfo" || local == "documentation" {
return
}
let isSchema = node.name?.namespaceURI == xsdNamespace && local == "schema"
let isAttribute = node.name?.namespaceURI == xsdNamespace && local == "attribute"
if isAttribute, PureXML.Schema.XSDNode.attribute(node, "ref") == nil, let name = PureXML.Schema.XSDNode.attribute(node, "name") {
let form = PureXML.Schema.XSDNode.attribute(node, "form")
let qualified = parentIsSchema || form == "qualified" || (form == nil && attributeFormDefault == "qualified")
if qualified {
findings.append(PureXML.Schema.SchemaLocatedFinding(
reason: "attribute declaration '\(name)' is in the XSI namespace, which is not allowed (xsi: Not Allowed)",
node: node,
))
}
}
for child in PureXML.Schema.XSDNode.elementChildren(node) {
collectXSIAttributeFindings(child, parentIsSchema: isSchema, attributeFormDefault: attributeFormDefault, into: &findings)
}
}
}