-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added CaseOf Validation with tests Signed-off-by: Mohammad <porooshani@gmail.com> * Changed caseOf to case and improved error messages and updated tests. Signed-off-by: Mohammad <porooshani@gmail.com> * Removed non-CaseIterable support from case(of:) validation. Signed-off-by: Mohammad <porooshani@gmail.com> * Removed unwanted map in error generator Signed-off-by: Mohammad <porooshani@gmail.com> * Improved error Generation. Signed-off-by: Mohammad <porooshani@gmail.com> * Add myself as a maintainer (#2188) * Adds EndpointCache for handling GET resource caching (#2184) * Adds a generic EndpointCache<T> class * Review fixups * Changed internal to private * Changed internal to private * Renamed the error * add EndpointCache tests * add CacheControl convienience init * rename badJSON error case Co-authored-by: Tanner <me@tanner.xyz> * expose methods for hex encoded strings (#2189) * Removed non-CaseIterable support from case(of:) validation. Signed-off-by: Mohammad <porooshani@gmail.com> * Removed unwanted map in error generator Signed-off-by: Mohammad <porooshani@gmail.com> * Improved error Generation. Signed-off-by: Mohammad <porooshani@gmail.com> * Improved description message for case(of:) Signed-off-by: Mohammad <porooshani@gmail.com> Co-authored-by: Joannis Orlandos <joannis@orlandos.nl> Co-authored-by: grosch <scott.grosch@icloud.com> Co-authored-by: Tanner <me@tanner.xyz>
- Loading branch information
1 parent
7bd5d33
commit 71cd6a7
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
extension Validator { | ||
/// Validates that the data can be converted to a value of an enum type with iterable cases. | ||
public static func `case`<E>(of enum: E.Type) -> Validator<T> | ||
where E: RawRepresentable & CaseIterable, E.RawValue == T, T: CustomStringConvertible | ||
{ | ||
.init { | ||
ValidatorResults.Case(enumType: E.self, rawValue: $0) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension ValidatorResults { | ||
/// `ValidatorResult` of a validator thaat validates whether the data can be represented as a specific Enum case. | ||
public struct Case<T, E> where E: RawRepresentable & CaseIterable, E.RawValue == T, T: CustomStringConvertible { | ||
/// The type of the enum to check. | ||
let enumType: E.Type | ||
/// The raw value that would be tested agains the enum type. | ||
let rawValue: T | ||
} | ||
} | ||
|
||
extension ValidatorResults.Case: ValidatorResult { | ||
public var isFailure: Bool { | ||
return enumType.init(rawValue: rawValue) == nil | ||
} | ||
|
||
public var successDescription: String? { | ||
makeDescription(not: false) | ||
} | ||
|
||
public var failureDescription: String? { | ||
makeDescription(not: true) | ||
} | ||
|
||
func makeDescription(not: Bool) -> String { | ||
let items = E.allCases.map { "\($0.rawValue)" } | ||
let description: String | ||
switch items.count { | ||
case 1: | ||
description = items[0].description | ||
case 2: | ||
description = "\(items[0].description) or \(items[1].description)" | ||
default: | ||
let first = items[0..<(items.count - 1)] | ||
.map { $0.description }.joined(separator: ", ") | ||
let last = items[items.count - 1].description | ||
description = "\(first), or \(last)" | ||
} | ||
return "is\(not ? " not" : "") \(description)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters