Slice 2 of #94. One call for an editor: PureXML.lint reads the document once into the ranged, best-effort tree (never throwing), then merges the parse diagnostics and the validation findings into a single source-ranged, severity-tagged list in document order. - LintDiagnostic carries message, severity, and the SourceRange to highlight. - lint(_:validate:) takes a node-based validate closure that runs over the RECOVERED tree, so validation works even when the document is not well-formed: a truncated, schema-invalid document yields both a parse diagnostic and the validation finding, each with a range. - Schema.Document gains validate(_ node:) so a caller validates the same tree it is editing; validate(_ xml:) delegates to it. Tests cover a clean lint, well-formedness-only lint, a range-located validation finding, and the both-truncated-and-invalid editor case.
63 lines
3.0 KiB
Swift
63 lines
3.0 KiB
Swift
public extension PureXML.Schema {
|
|
/// An error compiling or applying a schema.
|
|
enum SchemaError: Swift.Error, Equatable, Sendable, CustomStringConvertible {
|
|
case notASchema
|
|
|
|
public var description: String {
|
|
switch self {
|
|
case .notASchema: "the document is not an xs:schema"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A compiled XSD schema: its global element declarations and named-type
|
|
/// table, parsed from a schema document, used to validate instance documents.
|
|
struct Document: Sendable {
|
|
private let elements: [String: ElementType]
|
|
private let types: [String: ElementType]
|
|
private let constraints: [String: [IdentityConstraint]]
|
|
private let nillableElements: Set<String>
|
|
private let elementConstraints: [String: ValueConstraint]
|
|
|
|
/// Compiles a schema document. `schemaLoader` resolves the
|
|
/// `schemaLocation` of `xs:include`, `xs:import`, and `xs:redefine` to
|
|
/// schema source; it returns nil (the default) when external schemas are
|
|
/// not available, which keeps compilation from reaching the filesystem or
|
|
/// network by default.
|
|
public init(_ xsd: String, schemaLoader: @escaping (String) -> String? = { _ in nil }) throws {
|
|
let compiled = try XSDParser.parse(xsd, loader: schemaLoader)
|
|
elements = compiled.elements
|
|
types = compiled.types
|
|
constraints = compiled.constraints
|
|
nillableElements = compiled.nillableElements
|
|
elementConstraints = compiled.elementConstraints
|
|
}
|
|
|
|
/// Validates an instance document against the schema, returning one located
|
|
/// ``PureXML/Validation/ValidationError`` per violation. Reports an error
|
|
/// when the root element has no global declaration.
|
|
public func validate(_ xml: String) throws -> [PureXML.Validation.ValidationError] {
|
|
try validate(PureXML.parse(xml))
|
|
}
|
|
|
|
/// Validates an already-parsed node tree, so a caller can validate the same
|
|
/// (possibly recovered) tree it is editing. See ``validate(_:)-(String)``.
|
|
public func validate(_ node: PureXML.Model.Node) -> [PureXML.Validation.ValidationError] {
|
|
guard case let .document(children) = node, let root = children.compactMap(\.element).first else {
|
|
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: [])]
|
|
}
|
|
let context = PureXML.Validation.XSDContext(
|
|
types: types,
|
|
constraints: constraints,
|
|
rootDeclaration: declaration,
|
|
nillableElements: nillableElements,
|
|
elementConstraints: elementConstraints,
|
|
)
|
|
return PureXML.Validation.XSD.validator().errors(for: .element(root), in: context)
|
|
}
|
|
}
|
|
}
|