feat(schematron): schema- and pattern-level let
<let> may now appear at schema and pattern level, not just in a rule. Schema lets are evaluated once at the document root; pattern lets extend them (also at the root); rule lets extend those at each context node. A narrower-scope binding overrides a wider one of the same name. Part of #80
This commit is contained in:
parent
7a48b6c9e6
commit
b3d535c77e
@ -28,7 +28,7 @@ public extension PureXML.Validation {
|
||||
/// `#ALL` runs every pattern.
|
||||
public func validate(_ xml: String, phase: String? = nil) throws -> [ValidationError] {
|
||||
let node = try PureXML.parse(xml)
|
||||
let validation = Self.rule(activePatterns(phase: phase), diagnostics: schema.diagnostics)
|
||||
let validation = Self.rule(activePatterns(phase: phase), schemaLets: schema.lets, diagnostics: schema.diagnostics)
|
||||
return Validator<Void>.blank.validating(validation).errors(for: node, in: ())
|
||||
}
|
||||
|
||||
@ -45,10 +45,18 @@ public extension PureXML.Validation {
|
||||
|
||||
/// The schema as a single ``Validation`` over the document root, so it
|
||||
/// composes with the rest of the framework.
|
||||
static func rule(_ patterns: [SchematronPattern], diagnostics: [String: [SchematronMessagePart]]) -> Validation<PureXML.Model.Node, Void> {
|
||||
static func rule(
|
||||
_ patterns: [SchematronPattern],
|
||||
schemaLets: [SchematronLet],
|
||||
diagnostics: [String: [SchematronMessagePart]],
|
||||
) -> Validation<PureXML.Model.Node, Void> {
|
||||
.init(
|
||||
description: "Document satisfies the Schematron schema",
|
||||
check: { context in evaluate(patterns, over: PureXML.Model.TreeNode(context.subject), diagnostics: diagnostics) },
|
||||
check: { context in
|
||||
let root = PureXML.Model.TreeNode(context.subject)
|
||||
let base = bindings(schemaLets, at: root, base: [:])
|
||||
return evaluate(patterns, over: root, base: base, diagnostics: diagnostics)
|
||||
},
|
||||
when: { $0.codingPath.isEmpty },
|
||||
)
|
||||
}
|
||||
@ -56,30 +64,36 @@ public extension PureXML.Validation {
|
||||
private static func evaluate(
|
||||
_ patterns: [SchematronPattern],
|
||||
over root: PureXML.Model.TreeNode,
|
||||
base: [String: PureXML.XPath.Value],
|
||||
diagnostics: [String: [SchematronMessagePart]],
|
||||
) -> [ValidationError] {
|
||||
patterns.flatMap { evaluate($0, over: root, diagnostics: diagnostics) }
|
||||
patterns.flatMap { evaluate($0, over: root, base: base, diagnostics: diagnostics) }
|
||||
}
|
||||
|
||||
private static func evaluate(
|
||||
_ pattern: SchematronPattern,
|
||||
over root: PureXML.Model.TreeNode,
|
||||
base: [String: PureXML.XPath.Value],
|
||||
diagnostics: [String: [SchematronMessagePart]],
|
||||
) -> [ValidationError] {
|
||||
// Pattern-level lets extend the schema-level base, evaluated at the root.
|
||||
let patternBase = bindings(pattern.lets, at: root, base: base)
|
||||
var errors: [ValidationError] = []
|
||||
var claimed: Set<ObjectIdentifier> = []
|
||||
for rule in pattern.rules {
|
||||
for node in rule.context.nodes(over: root) where claimed.insert(ObjectIdentifier(node)).inserted {
|
||||
errors += apply(rule.assertions, at: node, variables: bindings(rule.lets, at: node), diagnostics: diagnostics)
|
||||
let variables = bindings(rule.lets, at: node, base: patternBase)
|
||||
errors += apply(rule.assertions, at: node, variables: variables, diagnostics: diagnostics)
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
/// Evaluates a rule's `<let>` bindings at the context node, in order, so a
|
||||
/// later binding can reference an earlier one through `$name`.
|
||||
private static func bindings(_ lets: [SchematronLet], at node: PureXML.Model.TreeNode) -> [String: PureXML.XPath.Value] {
|
||||
var variables: [String: PureXML.XPath.Value] = [:]
|
||||
/// Evaluates `<let>` bindings over `base` at `node`, in order, so a later
|
||||
/// binding can reference an earlier one (or a wider-scope one) through
|
||||
/// `$name`.
|
||||
private static func bindings(_ lets: [SchematronLet], at node: PureXML.Model.TreeNode, base: [String: PureXML.XPath.Value]) -> [String: PureXML.XPath.Value] {
|
||||
var variables = base
|
||||
for binding in lets {
|
||||
if let value = try? binding.value.value(at: node, variables: variables) {
|
||||
variables[binding.name] = value
|
||||
|
||||
@ -40,6 +40,7 @@ extension PureXML.Validation {
|
||||
/// context selects it (the ISO Schematron firing rule).
|
||||
struct SchematronPattern {
|
||||
let id: String?
|
||||
let lets: [SchematronLet]
|
||||
let rules: [SchematronRule]
|
||||
}
|
||||
|
||||
@ -49,6 +50,9 @@ extension PureXML.Validation {
|
||||
let patterns: [SchematronPattern]
|
||||
let phases: [String: [String]]
|
||||
let defaultPhase: String?
|
||||
/// Schema-level `<let>` bindings, evaluated once at the document root and
|
||||
/// available to every rule's tests.
|
||||
let lets: [SchematronLet]
|
||||
/// `<diagnostic id=>` message templates, keyed by id, referenced from an
|
||||
/// assertion's `diagnostics=` attribute.
|
||||
let diagnostics: [String: [SchematronMessagePart]]
|
||||
|
||||
@ -19,6 +19,7 @@ extension PureXML.Validation {
|
||||
patterns: patterns,
|
||||
phases: phases(root),
|
||||
defaultPhase: attribute(schema, "defaultPhase"),
|
||||
lets: ruleLets(schema),
|
||||
diagnostics: diagnostics(root),
|
||||
)
|
||||
}
|
||||
@ -43,7 +44,7 @@ extension PureXML.Validation {
|
||||
let rules = try ruleNodes
|
||||
.filter { attribute($0, "abstract") != "true" }
|
||||
.compactMap { try rule($0, abstracts) }
|
||||
return SchematronPattern(id: attribute(node, "id"), rules: rules)
|
||||
return try SchematronPattern(id: attribute(node, "id"), lets: ruleLets(node), rules: rules)
|
||||
}
|
||||
|
||||
private static func rule(_ node: PureXML.Model.TreeNode, _ abstracts: [String: AbstractRule]) throws -> SchematronRule? {
|
||||
|
||||
58
Tests/SchematronScopedLetTests.swift
Normal file
58
Tests/SchematronScopedLetTests.swift
Normal file
@ -0,0 +1,58 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
@Suite("Schematron schema- and pattern-level let")
|
||||
struct SchematronScopedLetTests {
|
||||
private func validate(_ schema: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] {
|
||||
try PureXML.Validation.Schematron(schema: schema).validate(xml)
|
||||
}
|
||||
|
||||
@Test("A schema-level let is available to every rule")
|
||||
func test_schemaLet() throws {
|
||||
let schema = """
|
||||
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
|
||||
<let name="limit" value="10"/>
|
||||
<pattern>
|
||||
<rule context="n">
|
||||
<assert test="number(.) < $limit">over the schema limit</assert>
|
||||
</rule>
|
||||
</pattern>
|
||||
</schema>
|
||||
"""
|
||||
#expect(try validate(schema, "<n>5</n>").isEmpty)
|
||||
#expect(try !validate(schema, "<n>50</n>").isEmpty)
|
||||
}
|
||||
|
||||
@Test("A pattern-level let is available to the pattern's rules")
|
||||
func test_patternLet() throws {
|
||||
let schema = """
|
||||
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
|
||||
<pattern>
|
||||
<let name="want" value="'yes'"/>
|
||||
<rule context="flag">
|
||||
<assert test=". = $want">flag must be yes</assert>
|
||||
</rule>
|
||||
</pattern>
|
||||
</schema>
|
||||
"""
|
||||
#expect(try validate(schema, "<flag>yes</flag>").isEmpty)
|
||||
#expect(try !validate(schema, "<flag>no</flag>").isEmpty)
|
||||
}
|
||||
|
||||
@Test("A rule-level let overrides a wider-scope let of the same name")
|
||||
func test_ruleOverridesSchema() throws {
|
||||
let schema = """
|
||||
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
|
||||
<let name="limit" value="100"/>
|
||||
<pattern>
|
||||
<rule context="n">
|
||||
<let name="limit" value="10"/>
|
||||
<assert test="number(.) < $limit">over the rule limit</assert>
|
||||
</rule>
|
||||
</pattern>
|
||||
</schema>
|
||||
"""
|
||||
// The rule's limit (10) wins over the schema's (100).
|
||||
#expect(try !validate(schema, "<n>50</n>").isEmpty)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user