Changed ElementValidation from a protocol to a struct

This commit is contained in:
Caleb Kleveter 2018-11-28 16:02:36 -06:00
parent f1bcd4ef2c
commit 3573962d26
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
4 changed files with 13 additions and 29 deletions

View File

@ -34,28 +34,13 @@ extension LengthValidation {
}
}
/// Validates each element in a sequence using a custom validation function.
/// Validates each element in a sequence using another validator.
///
/// struct StringLengthArray: ElementValidation {
/// typealias Supported = [String]
///
/// static var validator: (String)throws -> Void = { str in
/// guard str.count <= 1028 else { throw ValidationError(identifier: "lengthToLong", reason: "String must have length 1028 or less") }
/// }
/// }
///
/// The `validate` method calls `.forEach` on the value passed in and passes the `validator` function in as the closure.
public protocol ElementValidation: Validation where Supported: Sequence {
/// The `validate` method calls `.forEach` on the value passed in and passes the `Validator.run` method in as the closure.
public struct ElementValidation<Seq, Validator>: Validation where Seq: Sequence, Validator: Validation, Validator.Supported == Seq.Element {
public typealias Supported = Seq
/// The function used to validate each element in the sequence.
static var validator: (Supported.Element)throws -> Void { get }
}
extension ElementValidation {
/// See `Validation.validate(_:)`.
static func validate(_ value: Supported)throws {
return try value.forEach(self.validator)
public static func validate(_ value: Seq)throws {
try value.forEach(Validator.run)
}
}

View File

@ -3,7 +3,7 @@ import XCTest
final class FailableTests: XCTestCase {
func testInit()throws {
var story = try Failable<String, EmptyValidation<String>>("Hello world...")
var story: Failable<String, USPhoneNumber> = try "Hello world...".failable()
XCTAssertEqual(story.value, "Hello world...")
story = try "Long long ago...".failable()
@ -11,7 +11,7 @@ final class FailableTests: XCTestCase {
}
func testSet()throws {
var story = try Failable<String, EmptyValidation<String>>("Hello world...")
var story: Failable<String, USPhoneNumber> = try "Hello world...".failable()
try story <~ "Long long ago..."
XCTAssertEqual(story.value, "Long long ago...")

View File

@ -8,13 +8,12 @@ internal struct LengthRange10To1028<C>: LengthValidation where C: Collection {
static var minLength: Int { return 10 }
}
internal struct StringLengthArray: ElementValidation {
typealias Supported = [String]
internal struct Length1028<C>: LengthValidation where C: Collection {
typealias Supported = C
static var validator: (String)throws -> Void = { str in
guard str.count <= 1028 else { throw ValidationError(identifier: "lengthToLong", reason: "String must have length 1028 or less") }
}
static var maxLength: Int { return 1028 }
}
internal typealias StringLengthArray = ElementValidation<[String], Length1028<String>>
final class CollectionTests: XCTestCase {
func testLengthValidation()throws {

View File

@ -7,7 +7,7 @@ internal struct USPhoneNumber: RegexValidation {
final class StringTests: XCTestCase {
func testUSPhoneNumber()throws {
var number = try Failable<String, USPhoneNumber>("731-943-4316")
var number: Failable<String, USPhoneNumber> = try "731-943-4316".failable()
try number <~ "(731)-943-4316"
try number <~ "1-731-943-4316"