Created
October 22, 2014 07:17
-
-
Save evgeniyd/7306308493319bca02f0 to your computer and use it in GitHub Desktop.
Fields (email, password, set password, form, etc.) validators. Based on WWDC2014 session #229. I'm still working on this.
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
import Foundation | |
protocol Validator { | |
func validateWithError(error: NSErrorPointer) -> Bool | |
} | |
/////////////////////////////////////////////// | |
// MARK: - PasswordValidator - | |
/////////////////////////////////////////////// | |
class PasswordValidator: Validator { | |
// init(password: String?) { | |
// input = password | |
// } | |
var input: String? | |
func validateWithError(error: NSErrorPointer) -> Bool { | |
if let nonEmptyInput = input { | |
var regexpError: NSError? | |
if let regexp = NSRegularExpression(pattern: "[a-zA-Z0-9_]{4,}", options:NSRegularExpressionOptions(0), error:®expError) { | |
let inputRange = NSRange(location:0, length:countElements(nonEmptyInput)) | |
let result = regexp.rangeOfFirstMatchInString(nonEmptyInput, options: .Anchored, range: inputRange) | |
if result.location == NSNotFound { | |
// regexp validation failed | |
// TODO: create error object for it | |
return false | |
} | |
else { | |
return true | |
} | |
} | |
else { | |
// regexp init error | |
if error != nil { error.memory = regexpError } | |
return false | |
} | |
} | |
// empty input | |
// Note: Regexp enforces to use at least 4 characters, so not-nil check might be not needed | |
// TODO: create error object for it | |
return false | |
} | |
} | |
/////////////////////////////////////////////// | |
// MARK: - UsernameValidator - | |
/////////////////////////////////////////////// | |
// Note: the username here means e-mail | |
class UsernameValidator: Validator { | |
var input: String? | |
func validateWithError(error: NSErrorPointer) -> Bool { | |
if let nonEmptyInput = input { | |
let regexPattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" | |
if let regexPredicate = NSPredicate(format:"SELF MATCHES %@", regexPattern) { | |
let result = regexPredicate.evaluateWithObject(nonEmptyInput) | |
// evaluating w/ regexp | |
return result | |
} | |
else { | |
// regexp creation error | |
return false | |
} | |
} | |
// empty input | |
return false | |
} | |
} | |
/////////////////////////////////////////////// | |
// MARK: - SetPasswordValidator - | |
/////////////////////////////////////////////// | |
class SetPasswordValidator: Validator { | |
let firstPasswordValidator = PasswordValidator() | |
let secondPasswordValidator = PasswordValidator() | |
func validateWithError(error: NSErrorPointer) -> Bool { | |
if firstPasswordValidator.validateWithError(nil) && secondPasswordValidator.validateWithError(nil) { | |
if firstPasswordValidator.input == secondPasswordValidator.input { | |
return true | |
} | |
} | |
return false | |
} | |
} | |
/////////////////////////////////////////////// | |
// MARK: - SignIn Form Validator - | |
/////////////////////////////////////////////// | |
// TODO: complete the form | |
class SignInFormValidator: Validator { | |
func validateWithError(error: NSErrorPointer) -> Bool { | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment