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 = """