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).
27 lines
1.3 KiB
Swift
27 lines
1.3 KiB
Swift
extension PureYAML.Model.Value: PureYAML.Validation.Traversable {
|
|
/// For a mapping: the mapping itself (a `Validation<Mapping, _>` fires on this), and each pair's
|
|
/// key node at its own path (so a key-typed check can inspect it, e.g. "is this key a string").
|
|
/// Nothing extra for any other case.
|
|
public var extraOffers: [(component: PureYAML.Validation.Path.Component?, value: any PureYAML.Validation.Validatable)] {
|
|
guard case let .mapping(mapping) = self else {
|
|
return []
|
|
}
|
|
return [(nil, mapping)] + mapping.pairs.map { pair in
|
|
(component: pair.keyNode.pathComponent, value: pair.keyNode)
|
|
}
|
|
}
|
|
|
|
/// A mapping's children are its pairs' values, keyed by their key node's path component; a
|
|
/// sequence's children are its values, keyed by index. A scalar has none.
|
|
public var children: [(component: PureYAML.Validation.Path.Component, value: PureYAML.Model.Value)] {
|
|
switch self {
|
|
case let .mapping(mapping):
|
|
mapping.pairs.map { pair in (component: pair.keyNode.pathComponent, value: pair.value) }
|
|
case let .sequence(values):
|
|
values.indices.map { index in (component: .index(index), value: values[index]) }
|
|
case .null, .bool, .int, .double, .string:
|
|
[]
|
|
}
|
|
}
|
|
}
|