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.
29 lines
1.2 KiB
Swift
29 lines
1.2 KiB
Swift
public extension PureYAML.Validation {
|
|
/// A type-erased `Check`, so checks specialised on different subject types live in one list, all
|
|
/// sharing one `Document` (the validated tree's root type). It filters by runtime type: a
|
|
/// `Check<Subject, Document>` fires only on a `Subject` value, never on an optional of it (the
|
|
/// `type(of:)` guard) or a value of any other type.
|
|
struct AnyCheck<Document: Sendable>: Sendable {
|
|
public let description: String
|
|
private let applyErased: @Sendable (any Validatable, Path, Document) -> [Issue]
|
|
|
|
public init<Subject: Validatable>(_ check: Check<Subject, Document>) {
|
|
description = check.description
|
|
applyErased = { subject, path, document in
|
|
guard let typed = subject as? Subject, type(of: subject) == Subject.self else {
|
|
return []
|
|
}
|
|
return check.apply(SubjectContext(document: document, subject: typed, path: path))
|
|
}
|
|
}
|
|
|
|
public func apply(
|
|
to subject: any Validatable,
|
|
at path: Path,
|
|
in document: Document,
|
|
) -> [Issue] {
|
|
applyErased(subject, path, document)
|
|
}
|
|
}
|
|
}
|