Skip to content

Commit

Permalink
get ready to support ValidationErrorCollection as an OpenAPIError. Tw…
Browse files Browse the repository at this point in the history
…eak phrasing of errors that occur at the document root.
  • Loading branch information
mattpolzin committed Sep 5, 2023
1 parent 77df1d7 commit 352a9a0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
4 changes: 1 addition & 3 deletions Sources/OpenAPIKit/Validator/Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ public struct ValidationErrorCollection: Swift.Error, CustomStringConvertible, E

public var description: String { localizedDescription }

public var swiftErrors: [Swift.Error] {
return values
}
public var swiftErrors: [Swift.Error] { values }
}

/// Erases the type on which a `Validator` is specialized and combines
Expand Down
15 changes: 10 additions & 5 deletions Sources/OpenAPIKit30/Validator/Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,27 @@ public struct ValidationError: Swift.Error, CustomStringConvertible {
public var localizedDescription: String { description }

public var description: String {
"\(reason) at path: \(codingPath.stringValue)"
guard !codingPath.isEmpty else {
return "\(reason) at root of document"
}
return "\(reason) at path: \(codingPath.stringValue)"
}
}

/// Collects `ValidationErrors`.
///
/// This type is responsible for making it possible to collect validation
/// errors and throw one value (this collection) at the end of validation.
public struct ValidationErrorCollection: Swift.Error, CustomStringConvertible {
public struct ValidationErrorCollection: Swift.Error, CustomStringConvertible, ErrorCollection {
public let values: [ValidationError]

public var localizedDescription: String { description }

public var description: String {
public var localizedDescription: String {
return values.map(String.init(describing:)).joined(separator: "\n")
}

public var description: String { localizedDescription }

public var swiftErrors: [Swift.Error] { values }
}

/// Erases the type on which a `Validator` is specialized and combines
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenAPIKit30Tests/Validator/ValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ValidationTests: XCTestCase {
let check = AnyValidation(validation)

let errors = check.apply(to: "hello", at: [], in: testDocument)
XCTAssertEqual(errors.map { $0.description }, [ "because at path: " ])
XCTAssertEqual(errors.map { $0.description }, [ "because at root of document" ])

let string: String? = "hello"
let errors2 = check.apply(to: string as Any, at: [], in: testDocument)
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenAPIKitTests/Validator/ValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ValidationTests: XCTestCase {
let check = AnyValidation(validation)

let errors = check.apply(to: "hello", at: [], in: testDocument)
XCTAssertEqual(errors.map { $0.description }, [ "because at path: " ])
XCTAssertEqual(errors.map { $0.description }, [ "because at root of document" ])

let string: String? = "hello"
let errors2 = check.apply(to: string as Any, at: [], in: testDocument)
Expand Down

0 comments on commit 352a9a0

Please sign in to comment.