From f5059de42ed1c569c83f90ac750eba1a251ff137 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sun, 21 Jun 2026 13:57:35 +0200 Subject: [PATCH] feat(schema): locate ID-typed value-constraint errors Convert idValueConstraintErrors to idValueConstraintFindings so each a-props-correct.3 / e-props-correct.5 violation is attached to its declaring element or attribute node, letting an editor underline the offending declaration by line and column instead of the schema root. The error predicate and reason strings are unchanged: only where each error points differs. Adds a located-diagnostics test asserting the finding resolves to the declaring component's source range. --- Sources/Schema/SchemaCompileValidation.swift | 4 +--- Sources/Schema/SchemaIDValueConstraint.swift | 20 +++++++++++++------- Tests/SchemaLocatedDiagnosticsTests.swift | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index 2d96e41..5dca035 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -166,9 +166,7 @@ extension PureXML.Validation { static var idValueConstraintsValid: Validation { compileRule("ID-typed value constraints are valid") { document in - PureXML.Schema.SchemaLocatedFinding.unlocated( - PureXML.Schema.XSDParser.idValueConstraintErrors(document.schema, document.context), - ) + PureXML.Schema.XSDParser.idValueConstraintFindings(document.schema, document.context) } } diff --git a/Sources/Schema/SchemaIDValueConstraint.swift b/Sources/Schema/SchemaIDValueConstraint.swift index 1ba4d48..c2eb9e7 100644 --- a/Sources/Schema/SchemaIDValueConstraint.swift +++ b/Sources/Schema/SchemaIDValueConstraint.swift @@ -7,20 +7,26 @@ extension PureXML.Schema.XSDParser { /// named user type derived from `xs:ID` is recognized, not only the built-in and /// inline forms. Only XSD-namespace declarations are checked; foreign content is /// skipped. - static func idValueConstraintErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] { - var errors: [String] = [] - collectIDValueConstraintErrors(schema, context, into: &errors) - return errors + /// Located findings (#169): each error is attached to the declaring `element`/ + /// `attribute` node, so an editor can underline it by line/column. + static func idValueConstraintFindings(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [PureXML.Schema.SchemaLocatedFinding] { + var findings: [PureXML.Schema.SchemaLocatedFinding] = [] + collectIDValueConstraintFindings(schema, context, into: &findings) + return findings } - private static func collectIDValueConstraintErrors(_ node: XSDTree, _ context: PureXML.Schema.XSDContext, into errors: inout [String]) { + private static func collectIDValueConstraintFindings( + _ node: XSDTree, + _ context: PureXML.Schema.XSDContext, + into findings: inout [PureXML.Schema.SchemaLocatedFinding], + ) { let local = IDVNode.localName(node) if local == "appinfo" || local == "documentation" { return } if let error = idValueConstraintError(node, context) { - errors.append(error) + findings.append(PureXML.Schema.SchemaLocatedFinding(reason: error, node: node)) } for child in IDVNode.elementChildren(node) { - collectIDValueConstraintErrors(child, context, into: &errors) + collectIDValueConstraintFindings(child, context, into: &findings) } } diff --git a/Tests/SchemaLocatedDiagnosticsTests.swift b/Tests/SchemaLocatedDiagnosticsTests.swift index 0bd4c44..4246df9 100644 --- a/Tests/SchemaLocatedDiagnosticsTests.swift +++ b/Tests/SchemaLocatedDiagnosticsTests.swift @@ -55,6 +55,23 @@ struct SchemaLocatedDiagnosticsTests { #expect(range?.start.column == 3) } + @Test("An ID-typed default/fixed value constraint is located on its declaring component") + func test_idValueConstraintLocated() throws { + let xsd = """ + + + + """ + let findings = try inconsistentFindings(in: xsd) + let located = try #require(findings.first { $0.reason.contains("must not have a default or fixed value") }) + #expect(located.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"]) + + let (tree, _) = PureXML.readTree(xsd) + let range = tree.sourceRange(at: located.codingPath) + #expect(range?.start.line == 2) + #expect(range?.start.column == 3) + } + @Test("An xs:include targetNamespace mismatch is located on the include directive") func test_includeMismatchLocated() throws { let included = """