fix(xsd): report identity-constraint XPath compile errors
A selector or field XPath that fails to compile is now reported once up front as a located error (identity constraint 'k': invalid selector/field XPath '...'), instead of try? silently turning a schema author's typo into a disabled constraint. The per-run query cache from #112 is the choke point: compileErrors() probes every constraint's selector and fields in deterministic order, the failures cache as nil, and the walk afterwards treats those constraints as matching nothing, so the rest of the document still validates. Tests cover a malformed field and a malformed selector. Closes #111
This commit is contained in:
parent
7d3ef26558
commit
0edc1f77f9
@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- XSD identity-constraint XPath compile errors are no longer swallowed (#111): a selector or field XPath that fails to compile is reported once up front as a located error ("identity constraint 'k': invalid field XPath '...'"), instead of `try?` silently turning a schema author's typo into a disabled constraint. The broken query still evaluates as no-match afterwards, so the rest of the document is validated.
|
||||
- XSD type resolution no longer accepts invalid input silently (#110): an `xsi:type` naming an undeclared type is now a located error ("unknown xsi:type '...'") instead of a silent fallback to the declared type, in both the tree and streaming validators; and `typeReference` chains are resolved through one shared cycle-detecting resolver, so a circular chain reports "circular type reference '...'" instead of silently truncating after 64 hops (and no longer recurses without bound in the streaming shallow check, a latent crash). Completions use the same resolver, best-effort.
|
||||
- The HTML fragment parser (`HTML.parse`) now reconstructs active formatting elements, matching the document parser and the HTML5 algorithm (#109, surfaced by the Tier-2 conformance corpus). When a formatting element is closed out of order it is re-opened for the following content, so `<b><i></b>X</i>` yields `<b><i></i></b><i>X</i>` (not `<b><i></i></b>X`), in both the fragment and document paths.
|
||||
- XSD identity-constraint errors now locate the offending field, not just the element (#104): a single-field `key`/`keyref`/`unique` error extends the coding path with the field itself, an attribute (`... at path: list/item[2]/@id`) or a child element (`.../code`), resolved from the constraint's field XPath. keyref scope resolution is confirmed to resolve a key declared on an ancestor element (cross-scope), with a test. Full XSD qualified-node-set conformance for pathological multi-level same-name nesting remains part of the conformance-corpus work.
|
||||
|
||||
@ -99,7 +99,7 @@ flowchart TB
|
||||
|
||||
PureXML is a working, dependency-free XML library today: parse, emit, validate,
|
||||
query, and stream documents on macOS, Linux, Windows, and WASM. The test suite
|
||||
currently runs **825 tests in 131 suites** (`swift test`).
|
||||
currently runs **827 tests in 131 suites** (`swift test`).
|
||||
|
||||
### Shipped (libxml2-aligned surface)
|
||||
|
||||
|
||||
@ -34,11 +34,32 @@ extension PureXML.Schema {
|
||||
at path: [PureXML.Validation.PathKey] = [],
|
||||
) -> [PureXML.Validation.ValidationError] {
|
||||
guard !constraints.isEmpty else { return [] }
|
||||
var issues: [PureXML.Validation.ValidationError] = []
|
||||
// A selector or field XPath that does not compile is a schema-author
|
||||
// error, reported once up front rather than silently disabling the
|
||||
// constraint (the broken query still evaluates as no-match below).
|
||||
var issues: [PureXML.Validation.ValidationError] = compileErrors(at: path)
|
||||
walk(root, at: path, scopes: [], into: &issues)
|
||||
return issues
|
||||
}
|
||||
|
||||
/// One located error per constraint XPath that fails to compile, in a
|
||||
/// deterministic order. The failed compilations are cached, so the walk
|
||||
/// afterwards treats those constraints as matching nothing.
|
||||
private func compileErrors(at path: [PureXML.Validation.PathKey]) -> [PureXML.Validation.ValidationError] {
|
||||
var errors: [PureXML.Validation.ValidationError] = []
|
||||
for key in constraints.keys.sorted() {
|
||||
for constraint in constraints[key] ?? [] {
|
||||
if cache.query(constraint.selector) == nil {
|
||||
errors.append(.init(reason: "identity constraint '\(constraint.name)': invalid selector XPath '\(constraint.selector)'", at: path))
|
||||
}
|
||||
for field in constraint.fields where cache.query(field) == nil {
|
||||
errors.append(.init(reason: "identity constraint '\(constraint.name)': invalid field XPath '\(field)'", at: path))
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
private func walk(
|
||||
_ node: PureXML.Model.TreeNode,
|
||||
at path: [PureXML.Validation.PathKey],
|
||||
|
||||
@ -158,6 +158,38 @@ struct XSDIdentityTests {
|
||||
#expect(try validate(xsd, dangling).contains { $0.reason.contains("no matching key 'pk'") })
|
||||
}
|
||||
|
||||
@Test("A malformed field XPath is a located error, not a silently disabled constraint")
|
||||
func test_malformedFieldReported() throws {
|
||||
let xsd = """
|
||||
\(head)
|
||||
<xs:element name="list">
|
||||
<xs:complexType>
|
||||
<xs:sequence><xs:element name="item" type="xs:string" maxOccurs="unbounded"/></xs:sequence>
|
||||
</xs:complexType>
|
||||
<xs:key name="k"><xs:selector xpath="item"/><xs:field xpath="@id["/></xs:key>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
"""
|
||||
let errors = try validate(xsd, "<list><item id=\"1\"/><item id=\"1\"/></list>")
|
||||
#expect(errors.contains { $0.reason.contains("invalid field XPath '@id['") }, "\(errors.map(\.reason))")
|
||||
}
|
||||
|
||||
@Test("A malformed selector XPath is a located error")
|
||||
func test_malformedSelectorReported() throws {
|
||||
let xsd = """
|
||||
\(head)
|
||||
<xs:element name="list">
|
||||
<xs:complexType>
|
||||
<xs:sequence><xs:element name="item" type="xs:string" maxOccurs="unbounded"/></xs:sequence>
|
||||
</xs:complexType>
|
||||
<xs:key name="k"><xs:selector xpath="item["/><xs:field xpath="@id"/></xs:key>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
"""
|
||||
let errors = try validate(xsd, "<list><item id=\"1\"/></list>")
|
||||
#expect(errors.contains { $0.reason.contains("invalid selector XPath 'item['") }, "\(errors.map(\.reason))")
|
||||
}
|
||||
|
||||
@Test("A child-element field locates the error at that child")
|
||||
func test_childElementField() throws {
|
||||
let xsd = """
|
||||
|
||||
Loading…
Reference in New Issue
Block a user