Last active
February 5, 2019 02:27
-
-
Save up1/a0c3e18ad68650afb3a6a2e6e190fc13 to your computer and use it in GitHub Desktop.
Swift :: Validator input
This file contains hidden or 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
enum ValidateResult { | |
case valid | |
case invalid(error: Error) | |
} |
This file contains hidden or 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
enum UsernameValidateError: Error { | |
case empty | |
case tooShort | |
case tooLong | |
} |
This file contains hidden or 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
protocol Validator { | |
func validate(value: String) -> ValidateResult | |
} |
This file contains hidden or 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
struct EmptyValidator: Validator { | |
private let invalidError: Error | |
init(invalidError: Error) { | |
self.invalidError = invalidError | |
} | |
func validate(value: String) -> ValidateResult { | |
if value.isEmpty { | |
return .invalid(error: invalidError) | |
} | |
return .valid | |
} | |
} |
This file contains hidden or 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
struct UsernameTooShortValidator: Validator { | |
func validate(value: String) -> ValidateResult { | |
if value.characters.count < 5 { | |
return .invalid(error: UsernameValidateError.tooShort) | |
} | |
return .valid | |
} | |
} |
This file contains hidden or 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
struct UsernameTooLongValidator: Validator { | |
func validate(value: String) -> ValidateResult { | |
if value.characters.count > 20 { | |
return .invalid(error: UsernameValidateError.tooLong) | |
} | |
return .valid | |
} | |
} |
This file contains hidden or 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
struct Validators: Validator { | |
private let validators: [Validator] | |
init(validators: Validator...) { | |
self.validators = validators | |
} | |
func validate(value: String) -> ValidateResult { | |
for validator in validators { | |
switch validator.validate(value: value) { | |
case .valid: | |
break | |
case .invalid(let error): | |
return .invalid(error: error) | |
} | |
} | |
return .valid | |
} | |
} |
This file contains hidden or 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
let usernameValidator: Validator | |
= Validators(validators: | |
EmptyValidator(invalidError: UsernameValidateError.empty), | |
UsernameTooShortValidator(), | |
UsernameTooLongValidator() | |
) | |
print(usernameValidator.validate(value: "")) | |
print(usernameValidator.validate(value: "123")) | |
print(usernameValidator.validate(value: "123451234512345123451")) | |
print(usernameValidator.validate(value: "123456")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment