The tagged-node validator carried its own copy of the validation machinery: a Tagged.Rule cloning Validation's check type and a Tagged.Context cloning its context, differing only in subject type. That is the per-layer duplication the validation idiom exists to prevent (principle 10). Generalise the type-directed framework over both the subject and the document: SubjectContext, Check, and AnyCheck now take a Document type parameter for the validated tree's root. The model layer validates with a Model.Value document, the tagged layer with a Tagged.Node document, each truthful rather than forcing one root type to stand in for the other. The tagged layer keeps only what is genuinely its own: the Node document type, its built-in check (now Tagged.BuiltinCheck, a Check<Node, Node>), and its traversal. Tagged.Rule and the tagged Context are removed. The validator surface is unchanged but validating now takes a Check<Node, Node>. All 399 tests pass; the coverage gate reconciles to 62 public types.
23 lines
1.1 KiB
Swift
23 lines
1.1 KiB
Swift
public extension PureYAML.Validation {
|
|
/// The caseless namespace of built-in type-directed checks, referenceable by name.
|
|
enum BuiltinCheck {
|
|
/// Reports each duplicate key within a single mapping. The type-directed counterpart of the
|
|
/// legacy `Rule.mappingKeysAreUnique`; fires on `Model.Mapping` values directly rather than
|
|
/// switching on a `Model.Value`.
|
|
public static var mappingKeysAreUnique: Check<PureYAML.Model.Mapping, PureYAML.Model.Value> {
|
|
Check(description: "Mapping keys are unique") { context in
|
|
var seen = Set<PureYAML.Model.Key>()
|
|
var issues: [Issue] = []
|
|
for pair in context.subject.pairs where !seen.insert(pair.keyNode).inserted {
|
|
issues.append(Issue(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key '\(pair.keyNode.description)'",
|
|
path: context.path.appending(pair.keyNode.pathComponent),
|
|
))
|
|
}
|
|
return issues
|
|
}
|
|
}
|
|
}
|
|
}
|