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.
This commit is contained in:
parent
d195dd3fa1
commit
47960a85c8
@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Added
|
||||
|
||||
- The tagged validator now enforces mapping-key uniqueness, the invariant the coverage manifest
|
||||
already claimed for `Tagged.Mapping` and `Tagged.Node` but no check enforced. `Tagged.Mapping`
|
||||
keeps every parsed pair, so a duplicate key survives into the tree; `Tagged.BuiltinCheck`
|
||||
`.mappingKeysAreUnique` (a `Check<Mapping, Node>` on the one framework) reports each duplicate at
|
||||
its key path, exactly as the plain model does. It ships failing, near-miss, and nested fixtures,
|
||||
and the tagged validator's two built-in checks are pinned by description. The `Model.Value`
|
||||
coverage reason is corrected to note that merges and aliases are resolved at parse time, so they
|
||||
are not a validation-phase invariant: every `validated` manifest entry now names an invariant a
|
||||
check actually enforces.
|
||||
- A finding-coverage meta-test makes validation coverage a tested law: every rule in the default
|
||||
validator must ship a failing fixture and a near-miss (just-valid) fixture, and the meta-test
|
||||
asserts the fixture registry covers exactly the default rule set, naming any default rule without
|
||||
|
||||
@ -3,6 +3,24 @@ public extension PureYAML.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> {
|
||||
|
||||
@ -15,7 +15,10 @@ public extension PureYAML.Tagged {
|
||||
}
|
||||
|
||||
public static var defaultChecks: [PureYAML.Validation.AnyCheck<Node>] {
|
||||
[PureYAML.Validation.AnyCheck(BuiltinCheck.builtInTagsAreSupportedAndKindConsistent)]
|
||||
[
|
||||
PureYAML.Validation.AnyCheck(BuiltinCheck.builtInTagsAreSupportedAndKindConsistent),
|
||||
PureYAML.Validation.AnyCheck(BuiltinCheck.mappingKeysAreUnique),
|
||||
]
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@ -83,10 +86,12 @@ extension PureYAML.Tagged.Validator {
|
||||
path: PureYAML.Validation.Path,
|
||||
issues: inout [PureYAML.Validation.Issue],
|
||||
) {
|
||||
// The document is the whole tagged tree; each check filters by runtime type, so a
|
||||
// `Check<Node, Node>` fires on every node offered here.
|
||||
for check in checks {
|
||||
issues.append(contentsOf: check.apply(to: subject, at: path, in: root))
|
||||
// The document is the whole tagged tree; each check filters by runtime type. Offer the node,
|
||||
// and for a mapping the mapping too, so a `Check<Node, Node>` and a `Check<Mapping, Node>`
|
||||
// each fire on what they specialise on.
|
||||
offer(subject, at: path, root: root, issues: &issues)
|
||||
if case let .mapping(mapping) = subject {
|
||||
offer(mapping, at: path, root: root, issues: &issues)
|
||||
}
|
||||
|
||||
switch subject {
|
||||
@ -112,4 +117,15 @@ extension PureYAML.Tagged.Validator {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func offer(
|
||||
_ subject: any PureYAML.Validation.Validatable,
|
||||
at path: PureYAML.Validation.Path,
|
||||
root: PureYAML.Tagged.Node,
|
||||
issues: inout [PureYAML.Validation.Issue],
|
||||
) {
|
||||
for check in checks {
|
||||
issues.append(contentsOf: check.apply(to: subject, at: path, in: root))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,3 +9,4 @@ extension PureYAML.Model.Value: PureYAML.Validation.Validatable {}
|
||||
extension PureYAML.Model.Mapping: PureYAML.Validation.Validatable {}
|
||||
extension PureYAML.Model.Key: PureYAML.Validation.Validatable {}
|
||||
extension PureYAML.Tagged.Node: PureYAML.Validation.Validatable {}
|
||||
extension PureYAML.Tagged.Mapping: PureYAML.Validation.Validatable {}
|
||||
|
||||
49
Tests/TaggedKeyUniquenessTests.swift
Normal file
49
Tests/TaggedKeyUniquenessTests.swift
Normal file
@ -0,0 +1,49 @@
|
||||
@testable import PureYAML
|
||||
import Testing
|
||||
|
||||
/// The tagged validator enforces mapping-key uniqueness, the invariant the coverage manifest claims
|
||||
/// for `Tagged.Mapping` and `Tagged.Node`. `Tagged.Mapping` preserves every parsed pair, so a
|
||||
/// duplicate key survives into the tree and must be reported, exactly as in the plain model.
|
||||
@Suite("Tagged mapping key uniqueness")
|
||||
struct TaggedKeyUniquenessTests {
|
||||
@Test("A duplicate key in a tagged mapping is reported at the key's path")
|
||||
func test_reportsDuplicateTaggedKey() throws {
|
||||
let node = try PureYAML.parseTagged("a: 1\na: 2")
|
||||
#expect(PureYAML.Tagged.Validator().collect(node).issues == [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'a'",
|
||||
path: .init([.key("a")]),
|
||||
),
|
||||
])
|
||||
#expect(throws: PureYAML.Validation.Issue.Collection.self) {
|
||||
try PureYAML.Tagged.Validator().validate(node)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("The near miss, a tagged mapping whose keys differ by one character, passes")
|
||||
func test_uniqueTaggedKeysPass() throws {
|
||||
let node = try PureYAML.parseTagged("a: 1\nb: 2")
|
||||
_ = try PureYAML.Tagged.Validator().validate(node)
|
||||
}
|
||||
|
||||
@Test("A duplicate key nested under a tag is reported at its nested path")
|
||||
func test_reportsNestedDuplicateTaggedKey() throws {
|
||||
let node = try PureYAML.parseTagged("outer:\n a: 1\n a: 2")
|
||||
#expect(PureYAML.Tagged.Validator().collect(node).issues == [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'a'",
|
||||
path: .init([.key("outer"), .key("a")]),
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("The default tagged validator runs exactly its two built-in checks")
|
||||
func test_defaultTaggedCheckDescriptions() {
|
||||
#expect(PureYAML.Tagged.Validator().validationDescriptions == [
|
||||
"Built-in tags are supported and kind-consistent",
|
||||
"Mapping keys are unique",
|
||||
])
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,7 @@ PureYAML.Tagged excluded caseless namespace; holds no instances or state
|
||||
PureYAML.Validation excluded caseless namespace; holds no instances or state
|
||||
|
||||
# --- model ---
|
||||
PureYAML.Model.Value validated core YAML value; mapping-key uniqueness and merge/alias shape are runtime invariants
|
||||
PureYAML.Model.Value validated core YAML value; mapping-key uniqueness is a runtime invariant (merges and aliases are resolved at parse time)
|
||||
PureYAML.Model.Mapping validated mapping keys must be unique within a mapping
|
||||
PureYAML.Model.Pair excluded data-transfer key/value pair; no invariant beyond its components
|
||||
PureYAML.Model.Key excluded closed sum type (string/sequence/mapping); illegal states unrepresentable
|
||||
|
||||
|
Can't render this file because it has a wrong number of fields in line 3.
|
Loading…
Reference in New Issue
Block a user