fix(xsd): locate identity-constraint errors at the offending node

Refine the identity-constraint path: rather than locating at the element
that declares the key, walk from the constraint-bearing element down to the
selected target and report the actual offender, so a duplicate key renders
"at path: list/item[2]" and a dangling keyref "at path: orders/line".
Also locate the "no element declaration for the root" XSD error at the root
element, matching the namespace check beside it that already did so. Lock
both with coding-path assertions in the identity tests.
This commit is contained in:
Mihaela Mihaljevic 2026-06-09 08:57:33 +02:00
parent 9730903803
commit 36a40eec08
4 changed files with 40 additions and 8 deletions

View File

@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSD identity-constraint errors (`key`, `keyref`, `unique`) now carry a coding path locating the constraining element, instead of always rendering "at root of document". The `IdentityValidator` walk threads a `[PathKey]` the same way the content-model validator does, so a duplicate-key or dangling-keyref failure reports "... at path: list" rather than an empty path. This brings the identity-constraint rule into line with the validation framework's path-carrying contract, which every other rule already honored.
- XSD identity-constraint errors (`key`, `keyref`, `unique`) now carry a coding path locating the actual offending element, instead of always rendering "at root of document". The `IdentityValidator` walk threads a `[PathKey]` the same way the content-model validator does, and each error is located at the selected target node, so a duplicate-key failure reports "... at path: list/item[2]" and a dangling keyref "... at path: orders/line" rather than an empty path. This brings the identity-constraint rule into line with the validation framework's path-carrying contract, which every other rule already honored. The "no element declaration for the root" XSD error is likewise located at the root element, matching the namespace check beside it.
- A `<template>` element's flow content is now kept nested inside it (`<template><div>x</div></template>`) instead of being split out into the body (#83). `template` is built as body flow content rather than routed to the head.
### Added

View File

@ -62,6 +62,33 @@ extension PureXML.Schema {
}
}
/// The coding-path steps from the constraint-bearing `container` down to a
/// selected `target`, so an identity error locates the actual offending
/// element rather than the element that merely declares the constraint.
/// Returns an empty path if `target` is not a descendant of `container`.
private func steps(from container: PureXML.Model.TreeNode, to target: PureXML.Model.TreeNode) -> [PureXML.Validation.PathKey] {
var chain: [PureXML.Model.TreeNode] = []
var current = target
while current !== container {
guard let parent = current.parent else { return [] }
chain.append(current)
current = parent
}
var steps: [PureXML.Validation.PathKey] = []
var parent = container
for node in chain.reversed() {
let name = node.name?.description ?? ""
let siblings = parent.children.filter { $0.kind == .element && ($0.name?.description ?? "") == name }
if siblings.count > 1, let index = siblings.firstIndex(where: { $0 === node }) {
steps.append(.element(name, index: index + 1))
} else {
steps.append(.element(name))
}
parent = node
}
return steps
}
private func isNonRef(_ constraint: IdentityConstraint) -> Bool {
if case .keyref = constraint.kind { return false }
return true
@ -80,12 +107,12 @@ extension PureXML.Schema {
for target in select(constraint.selector, at: node) {
let tuple = fieldTuple(constraint.fields, at: target)
if constraint.kind == .key, tuple.contains(where: { $0 == nil }) {
issues.append(.init(reason: "key '\(constraint.name)': a field is missing", at: path))
issues.append(.init(reason: "key '\(constraint.name)': a field is missing", at: path + steps(from: node, to: target)))
continue
}
if tuple.contains(where: { $0 == nil }) { continue }
if seen.contains(where: { $0 == tuple }) {
issues.append(.init(reason: "\(label(constraint)) '\(constraint.name)': duplicate value", at: path))
issues.append(.init(reason: "\(label(constraint)) '\(constraint.name)': duplicate value", at: path + steps(from: node, to: target)))
} else {
seen.append(tuple)
}
@ -107,7 +134,7 @@ extension PureXML.Schema {
let tuple = fieldTuple(constraint.fields, at: target)
if tuple.contains(where: { $0 == nil }) { continue }
if !keyTuples.contains(where: { $0 == tuple }) {
issues.append(.init(reason: "keyref '\(constraint.name)': no matching key '\(refer)'", at: path))
issues.append(.init(reason: "keyref '\(constraint.name)': no matching key '\(refer)'", at: path + steps(from: node, to: target)))
}
}
}

View File

@ -73,7 +73,7 @@ public extension PureXML.Schema {
return [.init(reason: "the document has no root element", at: [])]
}
guard let declaration = elements[root.name.localName] else {
return [.init(reason: "no element declaration for '\(root.name.localName)'", at: [])]
return [.init(reason: "no element declaration for '\(root.name.localName)'", at: [.element(root.name.description)])]
}
// A schema with a target namespace declares its global elements in that
// namespace; the root must be in it.

View File

@ -76,7 +76,10 @@ struct XSDIdentityTests {
let valid = "<orders><product code=\"A\"/><product code=\"B\"/><line product=\"A\"/></orders>"
let dangling = "<orders><product code=\"A\"/><line product=\"Z\"/></orders>"
#expect(try validate(xsd, valid).isEmpty)
#expect(try !validate(xsd, dangling).isEmpty)
// The dangling keyref is located at the offending <line>, not <orders>.
let failure = try #require(validate(xsd, dangling).first)
#expect(failure.codingPath.map(\.stringValue) == ["orders", "line"])
#expect(String(describing: failure).hasSuffix("at path: orders/line"))
}
@Test("Identity-constraint errors are located at the constraining element")
@ -94,9 +97,11 @@ struct XSDIdentityTests {
</xs:element>
</xs:schema>
"""
// The error locates the actual offending element (the duplicate item[2]),
// not just the element that declares the key.
let errors = try validate(xsd, "<list><item id=\"1\"/><item id=\"1\"/></list>")
let failure = try #require(errors.first)
#expect(failure.codingPath.map(\.stringValue) == ["list"])
#expect(String(describing: failure).hasSuffix("at path: list"))
#expect(failure.codingPath.map(\.stringValue) == ["list", "item"])
#expect(String(describing: failure).hasSuffix("at path: list/item[2]"))
}
}