The validation module already implemented the composable-rule idiom under its own names (Check/Issue/SubjectContext) and a value-type Validator with duplicated traversal machinery per layer. Renamed the core types to the canonical names (Validation/ValidationError/ValidationErrorCollection/ ValidationContext/BuiltinValidation/AnyValidation), converted Validator to a reference-type class with the two-tier default-rule split, and genericized it over both Subject and Document via a new Traversable protocol so the model and tagged layers share one walk engine instead of two copies. Wired the JSON-representability rule set into the same shape and closed the two missing pieces (unwrap combinator, JSON coverage/config-pin tests).
29 lines
1.3 KiB
Swift
29 lines
1.3 KiB
Swift
public extension PureYAML.Validation {
|
|
/// A type-erased `Validation`, 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
|
|
/// `Validation<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 AnyValidation<Document: Sendable>: Sendable {
|
|
public let description: String
|
|
private let applyErased: @Sendable (any Validatable, Path, Document) -> [ValidationError]
|
|
|
|
public init<Subject: Validatable>(_ validation: Validation<Subject, Document>) {
|
|
description = validation.description
|
|
applyErased = { subject, path, document in
|
|
guard let typed = subject as? Subject, type(of: subject) == Subject.self else {
|
|
return []
|
|
}
|
|
return validation.apply(ValidationContext(document: document, subject: typed, path: path))
|
|
}
|
|
}
|
|
|
|
public func apply(
|
|
to subject: any Validatable,
|
|
at path: Path,
|
|
in document: Document,
|
|
) -> [ValidationError] {
|
|
applyErased(subject, path, document)
|
|
}
|
|
}
|
|
}
|