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).
94 lines
3.4 KiB
Swift
94 lines
3.4 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
@Suite("Validation Model")
|
|
struct ValidationModelTests {
|
|
@Test("Validation result separates errors and warnings")
|
|
func test_validationResultSeparatesErrorsAndWarnings() {
|
|
let error = PureYAML.Validation.ValidationError(severity: .error, reason: "error")
|
|
let warning = PureYAML.Validation.ValidationError(severity: .warning, reason: "warning")
|
|
let result = PureYAML.Validation.Result([error, warning])
|
|
|
|
#expect(result.isValid == false)
|
|
#expect(result.errors == [error])
|
|
#expect(result.warnings == [warning])
|
|
#expect(!result.errors.contains(warning))
|
|
#expect(!result.warnings.contains(error))
|
|
}
|
|
|
|
@Test("Validation paths describe roots keys and indexes", arguments: [
|
|
ValidationPathCase(
|
|
name: "root",
|
|
path: .root,
|
|
expectedDescription: "$",
|
|
isRoot: true,
|
|
),
|
|
ValidationPathCase(
|
|
name: "single key",
|
|
path: .root.appending(.key("title")),
|
|
expectedDescription: "$.title",
|
|
isRoot: false,
|
|
),
|
|
ValidationPathCase(
|
|
name: "nested index",
|
|
path: .root
|
|
.appending(.key("routes"))
|
|
.appending(.index(1))
|
|
.appending(.key("name")),
|
|
expectedDescription: "$.routes[1].name",
|
|
isRoot: false,
|
|
),
|
|
ValidationPathCase(
|
|
name: "quoted slash key",
|
|
path: .root
|
|
.appending(.key("/users"))
|
|
.appending(.key("get.method")),
|
|
expectedDescription: "$[\"/users\"][\"get.method\"]",
|
|
isRoot: false,
|
|
),
|
|
ValidationPathCase(
|
|
name: "quoted empty key",
|
|
path: .root.appending(.key("")),
|
|
expectedDescription: "$[\"\"]",
|
|
isRoot: false,
|
|
),
|
|
ValidationPathCase(
|
|
name: "escaped quoted key",
|
|
path: .root
|
|
.appending(.key("quote\"slash\\line\nnext\tend")),
|
|
expectedDescription: "$[\"quote\\\"slash\\\\line\\nnext\\tend\"]",
|
|
isRoot: false,
|
|
),
|
|
])
|
|
func test_validationPathsDescribeRootsKeysAndIndexes(testCase: ValidationPathCase) {
|
|
#expect(testCase.path.description == testCase.expectedDescription)
|
|
#expect(testCase.path.isRoot == testCase.isRoot)
|
|
}
|
|
|
|
@Test("Validation issue descriptions include severity reason and path")
|
|
func test_validationIssueDescriptionsIncludeSeverityReasonAndPath() {
|
|
let issue = PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key 'title'",
|
|
path: .init([.key("title")]),
|
|
)
|
|
|
|
#expect(issue.description == "Duplicate mapping key 'title' at path: $.title")
|
|
}
|
|
|
|
@Test("Validation issue collection describes all failures")
|
|
func test_validationIssueCollectionDescription() {
|
|
let collection = PureYAML.Validation.ValidationErrorCollection([
|
|
.init(severity: .error, reason: "first", path: .root),
|
|
.init(severity: .warning, reason: "second", path: .init([.key("name")])),
|
|
])
|
|
|
|
#expect(collection.description == """
|
|
first at root of document
|
|
second at path: $.name
|
|
""")
|
|
#expect(collection.description.contains("first"))
|
|
#expect(!collection.description.contains("info:"))
|
|
}
|
|
}
|