PureYAML/Sources/Tagged/Tagged.Check.Builtins.swift
Mihaela M 47960a85c8 feat(validation): enforce tagged mapping-key uniqueness
The coverage manifest claimed Tagged.Mapping and Tagged.Node validate key
uniqueness, but the tagged validator's only default check was the built-in
tag check: the claim was false. Tagged.Mapping keeps every parsed pair, so a
duplicate key survives into the tree and is as invalid as in the plain model.

Add Tagged.BuiltinCheck.mappingKeysAreUnique, a Check<Mapping, Node> on the
one validation framework, to the tagged validator's defaults; the traversal
now offers each mapping to the checks. It reports each duplicate at its key
path, with failing, near-miss, and nested fixtures, and the two default
tagged checks are pinned by description.

Correct the Model.Value coverage reason: merges and aliases resolve at parse
time, so they are not a validation-phase invariant. Every validated manifest
entry now names an invariant a check actually enforces. 403 tests pass.
2026-06-28 17:03:20 +02:00

107 lines
4.7 KiB
Swift

public extension PureYAML.Tagged {
/// The caseless namespace of built-in type-directed checks for the tagged-node tree, the tagged
/// layer's counterpart of `Validation.BuiltinCheck`. Each is a `Check<Node, Node>` on the one
/// shared validation framework, not a copy of the machinery (validation-rules.md, principle 10).
enum BuiltinCheck {
/// Reports each duplicate key within a single tagged mapping. The tagged-layer counterpart of
/// `Validation.BuiltinCheck.mappingKeysAreUnique`; duplicate keys are as invalid in a
/// tag-preserving tree as in the plain model, and `Tagged.Mapping` keeps every parsed pair.
public static var mappingKeysAreUnique: PureYAML.Validation.Check<Mapping, Node> {
PureYAML.Validation.Check(description: "Mapping keys are unique") { context in
var seen = Set<String>()
var issues: [PureYAML.Validation.Issue] = []
for pair in context.subject.pairs where !seen.insert(pair.key).inserted {
issues.append(PureYAML.Validation.Issue(
severity: .error,
reason: "Duplicate mapping key '\(pair.key)'",
path: context.path.appending(.key(pair.key)),
))
}
return issues
}
}
/// Reports unsupported built-in tags and built-in tags applied to the wrong node kind, on the
/// node itself and on each mapping key.
public static var builtInTagsAreSupportedAndKindConsistent: PureYAML.Validation.Check<Node, Node> {
PureYAML.Validation.Check(description: "Built-in tags are supported and kind-consistent") { context in
var issues: [PureYAML.Validation.Issue] = []
if case let .mapping(mapping) = context.subject {
issues.append(contentsOf: mapping.pairs.compactMap { pair in
keyTagIssue(for: pair, at: context.path.appending(.key(pair.key)))
})
}
guard let tag = context.subject.tag, tag.isBuiltIn else {
return issues
}
if let expectedKind = tag.expectedKind, expectedKind != context.subject.kind {
issues.append(
PureYAML.Validation.Issue(
severity: .error,
reason: "Tag '\(tag.rawValue)' expects a \(expectedKind) node, found \(context.subject.kind)",
path: context.path,
),
)
return issues
}
if !tag.isSupportedBuiltInTag {
issues.append(
PureYAML.Validation.Issue(
severity: .error,
reason: "Unsupported built-in tag '\(tag.rawValue)'",
path: context.path,
),
)
}
return issues
} when: { context in
if context.subject.tag?.isBuiltIn == true {
return true
}
if case let .mapping(mapping) = context.subject {
return mapping.pairs.contains { $0.keyTag?.isBuiltIn == true }
}
return false
}
}
static func keyTagIssue(
for pair: PureYAML.Tagged.Pair,
at path: PureYAML.Validation.Path,
) -> PureYAML.Validation.Issue? {
guard let tag = pair.keyTag, tag.isBuiltIn else {
return nil
}
if tag == .merge, pair.key == "<<" {
return nil
}
if tag == .merge {
return PureYAML.Validation.Issue(
severity: .error,
reason: "Tag '\(tag.rawValue)' is only supported on '<<' mapping keys",
path: path,
)
}
if let expectedKind = tag.expectedKind, expectedKind != .scalar {
return PureYAML.Validation.Issue(
severity: .error,
reason: "Tag '\(tag.rawValue)' expects a scalar mapping key",
path: path,
)
}
guard tag.isSupportedBuiltInTag else {
return PureYAML.Validation.Issue(
severity: .error,
reason: "Unsupported built-in tag '\(tag.rawValue)' on mapping key",
path: path,
)
}
return nil
}
}
}