diff --git a/CHANGELOG.md b/CHANGELOG.md index 711d5fb..d59e589 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `X` yields `X` (not `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. diff --git a/README.md b/README.md index 42345e8..a4575af 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/Sources/Schema/IdentityValidator.swift b/Sources/Schema/IdentityValidator.swift index 3c2c5bc..979dc57 100644 --- a/Sources/Schema/IdentityValidator.swift +++ b/Sources/Schema/IdentityValidator.swift @@ -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], diff --git a/Tests/XSDIdentityTests.swift b/Tests/XSDIdentityTests.swift index 1ce2c66..4e331c9 100644 --- a/Tests/XSDIdentityTests.swift +++ b/Tests/XSDIdentityTests.swift @@ -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) + + + + + + + + """ + let errors = try validate(xsd, "") + #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) + + + + + + + + """ + let errors = try validate(xsd, "") + #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 = """