From 47960a85c87274ce6e5b62feb8dc92cc92b66ab3 Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Sun, 28 Jun 2026 17:03:20 +0200 Subject: [PATCH] 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 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. --- CHANGELOG.md | 9 ++++ Sources/Tagged/Tagged.Check.Builtins.swift | 18 ++++++++ Sources/Tagged/Tagged.Validator.swift | 26 +++++++++--- Sources/Validation/Validatable.swift | 1 + Tests/TaggedKeyUniquenessTests.swift | 49 ++++++++++++++++++++++ scripts/validation-coverage.tsv | 2 +- 6 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 Tests/TaggedKeyUniquenessTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b18c7..2a75ec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` 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 diff --git a/Sources/Tagged/Tagged.Check.Builtins.swift b/Sources/Tagged/Tagged.Check.Builtins.swift index 03388d1..59cc676 100644 --- a/Sources/Tagged/Tagged.Check.Builtins.swift +++ b/Sources/Tagged/Tagged.Check.Builtins.swift @@ -3,6 +3,24 @@ public extension PureYAML.Tagged { /// layer's counterpart of `Validation.BuiltinCheck`. Each is a `Check` 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 { + PureYAML.Validation.Check(description: "Mapping keys are unique") { context in + var seen = Set() + 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 { diff --git a/Sources/Tagged/Tagged.Validator.swift b/Sources/Tagged/Tagged.Validator.swift index 34aac4f..49e27d5 100644 --- a/Sources/Tagged/Tagged.Validator.swift +++ b/Sources/Tagged/Tagged.Validator.swift @@ -15,7 +15,10 @@ public extension PureYAML.Tagged { } public static var defaultChecks: [PureYAML.Validation.AnyCheck] { - [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` 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` and a `Check` + // 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)) + } + } } diff --git a/Sources/Validation/Validatable.swift b/Sources/Validation/Validatable.swift index b428641..799659e 100644 --- a/Sources/Validation/Validatable.swift +++ b/Sources/Validation/Validatable.swift @@ -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 {} diff --git a/Tests/TaggedKeyUniquenessTests.swift b/Tests/TaggedKeyUniquenessTests.swift new file mode 100644 index 0000000..467937c --- /dev/null +++ b/Tests/TaggedKeyUniquenessTests.swift @@ -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", + ]) + } +} diff --git a/scripts/validation-coverage.tsv b/scripts/validation-coverage.tsv index d61feef..481ec9d 100644 --- a/scripts/validation-coverage.tsv +++ b/scripts/validation-coverage.tsv @@ -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