Move the validator toward the validation-rules idiom's identity-based configuration. withoutValidating(_:) removes a rule by its description, the removal identity the idiom uses, so descriptions must be unique and stable. validationDescriptions lists the active rules in application order. A configuration-pin test asserts the exact descriptions of the blank and default validators and that the default descriptions are unique, so adding, removing, or rewording a rule is a deliberate change that a test catches. Both additions are non-breaking; the type-directed Validation<Subject> dispatch and the error-rendering change remain the larger migration steps.
40 lines
1.6 KiB
Swift
40 lines
1.6 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
@Suite("Validator configuration")
|
|
struct ValidationConfigurationTests {
|
|
private var duplicateKeys: PureYAML.Model.Value {
|
|
.mapping(.init([
|
|
.init(key: "a", value: .int(1)),
|
|
.init(key: "a", value: .int(2)),
|
|
]))
|
|
}
|
|
|
|
@Test("The blank validator has no rules; the default set is pinned by description")
|
|
func test_configurationDescriptionsArePinned() {
|
|
#expect(PureYAML.Validation.Validator.blank.validationDescriptions == [])
|
|
#expect(PureYAML.Validation.Validator().validationDescriptions == ["Mapping keys are unique"])
|
|
}
|
|
|
|
@Test("Default rule descriptions are unique, the identity withoutValidating removes by")
|
|
func test_defaultDescriptionsAreUnique() {
|
|
let descriptions = PureYAML.Validation.Validator().validationDescriptions
|
|
#expect(Set(descriptions).count == descriptions.count)
|
|
}
|
|
|
|
@Test("withoutValidating removes a rule by its description")
|
|
func test_withoutValidatingRemovesByDescription() throws {
|
|
let validator = PureYAML.Validation.Validator().withoutValidating("Mapping keys are unique")
|
|
#expect(validator.validationDescriptions == [])
|
|
// With the rule removed, a document with duplicate keys validates clean.
|
|
_ = try validator.validate(duplicateKeys)
|
|
}
|
|
|
|
@Test("The default validator still catches what its removable rule guards")
|
|
func test_defaultStillValidates() {
|
|
#expect(throws: PureYAML.Validation.Issue.Collection.self) {
|
|
try PureYAML.Validation.Validator().validate(duplicateKeys)
|
|
}
|
|
}
|
|
}
|