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).
87 lines
3.3 KiB
Swift
87 lines
3.3 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
@Suite("Validation Built-in Rules")
|
|
struct ValidationBuiltinRuleTests {
|
|
@Test("Validates preserved value trees that ordinary YAML loaders may collapse")
|
|
func test_validatesPreservedValueTreesThatOrdinaryYAMLLoadersMayCollapse() {
|
|
let result = PureYAML.Validation.Validator<PureYAML.Model.Value, PureYAML.Model.Value>().collect(preservedDuplicateKeyValue)
|
|
|
|
#expect(result.values == expectedIssues)
|
|
#expect(result.values.map(\.description) == [
|
|
"Duplicate mapping key 'title' at path: $.title",
|
|
"Duplicate mapping key 'items' at path: $.items",
|
|
"Duplicate mapping key '/users' at path: $.paths[\"/users\"]",
|
|
"Duplicate mapping key 'name.with.dot' at path: $.items[0][\"name.with.dot\"]",
|
|
])
|
|
#expect(!result.values.contains(.init(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key 'paths'",
|
|
path: .init([.key("paths")]),
|
|
)))
|
|
expectValidationError(preservedDuplicateKeyValue) { collection in
|
|
#expect(collection.values == expectedIssues)
|
|
#expect(collection.description == expectedIssues.map(\.description).joined(separator: "\n"))
|
|
}
|
|
}
|
|
|
|
@Test("Mapping key uniqueness is scoped to mappings by type-directed dispatch")
|
|
func test_mappingKeyUniquenessIsScopedToMappings() {
|
|
let validator = PureYAML.Validation.Validator<PureYAML.Model.Value, PureYAML.Model.Value>()
|
|
|
|
// A scalar value carries no mapping, so the mapping-keyed check never fires on it.
|
|
#expect(validator.collect(.string("subject")).values.isEmpty)
|
|
|
|
// A mapping with a duplicate key reports exactly one issue, at the key's path.
|
|
#expect(validator.collect(.mapping(.init([
|
|
.init(key: "title", value: .string("one")),
|
|
.init(key: "title", value: .string("two")),
|
|
]))).values == [
|
|
.init(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key 'title'",
|
|
path: .init([.key("title")]),
|
|
),
|
|
])
|
|
}
|
|
}
|
|
|
|
private let preservedDuplicateKeyValue = PureYAML.Model.Value.mapping(.init([
|
|
.init(key: "title", value: .string("First")),
|
|
.init(key: "title", value: .string("Second")),
|
|
.init(key: "paths", value: .mapping(.init([
|
|
.init(key: "/users", value: .string("first")),
|
|
.init(key: "/users", value: .string("second")),
|
|
]))),
|
|
.init(key: "items", value: .sequence([
|
|
.mapping(.init([
|
|
.init(key: "name.with.dot", value: .string("one")),
|
|
.init(key: "name.with.dot", value: .string("two")),
|
|
])),
|
|
])),
|
|
.init(key: "items", value: .sequence([])),
|
|
]))
|
|
|
|
private let expectedIssues: [PureYAML.Validation.ValidationError] = [
|
|
.init(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key 'title'",
|
|
path: .init([.key("title")]),
|
|
),
|
|
.init(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key 'items'",
|
|
path: .init([.key("items")]),
|
|
),
|
|
.init(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key '/users'",
|
|
path: .init([.key("paths"), .key("/users")]),
|
|
),
|
|
.init(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key 'name.with.dot'",
|
|
path: .init([.key("items"), .index(0), .key("name.with.dot")]),
|
|
),
|
|
]
|