From 274556a57814505cfe0d48002f0c2b699ededaa4 Mon Sep 17 00:00:00 2001 From: Byron Mackay Date: Fri, 3 Apr 2015 14:26:51 -0600 Subject: [PATCH 1/3] Minor style changes --- Validator/ConfirmRule.swift | 7 +------ Validator/EmailRule.swift | 8 +++----- Validator/FullNameRule.swift | 13 ++++--------- Validator/MinLengthRule.swift | 16 +++++----------- Validator/PasswordRule.swift | 10 ++++------ Validator/RequiredRule.swift | 8 ++------ Validator/Rule.swift | 2 +- Validator/ValidationError.swift | 9 ++++----- Validator/ValidationRule.swift | 12 +++++------- Validator/Validator.swift | 26 ++++++++++---------------- Validator/ViewController.swift | 10 +--------- Validator/ZipCodeRule.swift | 4 ++-- 12 files changed, 42 insertions(+), 83 deletions(-) diff --git a/Validator/ConfirmRule.swift b/Validator/ConfirmRule.swift index e51d259..ba0a3bf 100644 --- a/Validator/ConfirmRule.swift +++ b/Validator/ConfirmRule.swift @@ -18,15 +18,10 @@ class ConfirmationRule: Rule { } func validate(value: String) -> Bool { - if self.confirmField.text == value { - return true - } else { - return false - } + return confirmField.text == value } func errorMessage() -> String { return "This field does not match" } - } \ No newline at end of file diff --git a/Validator/EmailRule.swift b/Validator/EmailRule.swift index 1d7f1bd..5c767fc 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -10,13 +10,11 @@ import Foundation class EmailRule: Rule { - private let REGEX: String + private let REGEX: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" - init(){ - self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" - } + init(){} - init(regex:String){ + init(regex: String){ REGEX = regex } diff --git a/Validator/FullNameRule.swift b/Validator/FullNameRule.swift index af9ea87..18b3a82 100644 --- a/Validator/FullNameRule.swift +++ b/Validator/FullNameRule.swift @@ -8,20 +8,15 @@ import Foundation - -class FullNameRule : Rule { +class FullNameRule: Rule { var message:String { return "Please provide a first & last name" } - func validate(value:String) -> Bool { - - var nameArray:[String] = split(value) { $0 == " " } - if nameArray.count >= 2 { - return true - } - return false + func validate(value: String) -> Bool { + var nameArray: [String] = split(value) { $0 == " " } + return nameArray.count >= 2 } func errorMessage() -> String { diff --git a/Validator/MinLengthRule.swift b/Validator/MinLengthRule.swift index 4971000..ba1bbc9 100644 --- a/Validator/MinLengthRule.swift +++ b/Validator/MinLengthRule.swift @@ -8,24 +8,18 @@ import Foundation - -class MinLengthRule : Rule { +class MinLengthRule: Rule { - private let DEFAULT_MIN_LENGTH: Int + private let DEFAULT_MIN_LENGTH: Int = 3 - init(){ - DEFAULT_MIN_LENGTH = 3 - } + init(){} - init(length:Int){ + init(length: Int){ self.DEFAULT_MIN_LENGTH = length } func validate(value: String) -> Bool { - if countElements(value) < DEFAULT_MIN_LENGTH { - return false - } - return true + return countElements(value) > DEFAULT_MIN_LENGTH } func errorMessage() -> String { diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index 122860d..f1e0c44 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -8,7 +8,7 @@ import Foundation -class PasswordRule : Rule { +class PasswordRule: Rule { // Alternative Regexes @@ -20,13 +20,11 @@ class PasswordRule : Rule { // 8 characters. one uppercase - private let REGEX: String + private let REGEX: String = "^(?=.*?[A-Z]).{8,}$" - init(){ - self.REGEX = "^(?=.*?[A-Z]).{8,}$" - } + init(){} - init(regex:String){ + init(regex: String){ self.REGEX = regex } diff --git a/Validator/RequiredRule.swift b/Validator/RequiredRule.swift index 3075912..31aadb5 100644 --- a/Validator/RequiredRule.swift +++ b/Validator/RequiredRule.swift @@ -8,7 +8,6 @@ import Foundation - class RequiredRule: Rule { init(){} @@ -17,11 +16,8 @@ class RequiredRule: Rule { return "This field is required" } - func validate(value:String) -> Bool { - if value.isEmpty { - return false - } - return true + func validate(value: String) -> Bool { + return !value.isEmpty } func errorMessage() -> String { diff --git a/Validator/Rule.swift b/Validator/Rule.swift index 2d97304..dfe5b0e 100644 --- a/Validator/Rule.swift +++ b/Validator/Rule.swift @@ -9,6 +9,6 @@ import Foundation protocol Rule { - func validate(value:String) -> Bool + func validate(value: String) -> Bool func errorMessage() -> String } diff --git a/Validator/ValidationError.swift b/Validator/ValidationError.swift index ba1e20b..f84abe6 100644 --- a/Validator/ValidationError.swift +++ b/Validator/ValidationError.swift @@ -10,13 +10,12 @@ import Foundation import UIKit class ValidationError { - let textField:UITextField - var errorLabel:UILabel? - let errorMessage:String + let textField: UITextField + let errorMessage: String + var errorLabel: UILabel? - init(textField:UITextField, error:String){ + init(textField: UITextField, error: String) { self.textField = textField self.errorMessage = error } - } \ No newline at end of file diff --git a/Validator/ValidationRule.swift b/Validator/ValidationRule.swift index bd11395..d6d772b 100644 --- a/Validator/ValidationRule.swift +++ b/Validator/ValidationRule.swift @@ -10,11 +10,11 @@ import Foundation import UIKit class ValidationRule { - var textField:UITextField - var errorLabel:UILabel? - var rules:[Rule] = [] + var textField: UITextField + var errorLabel: UILabel? + var rules: [Rule] = [] - init(textField: UITextField, rules:[Rule], errorLabel:UILabel?){ + init(textField: UITextField, rules: [Rule], errorLabel: UILabel?){ self.textField = textField self.errorLabel = errorLabel self.rules = rules @@ -22,12 +22,10 @@ class ValidationRule { func validateField() -> ValidationError? { for rule in rules { - var isValid:Bool = rule.validate(textField.text) - if !isValid { + if !rule.validate(textField.text) { return ValidationError(textField: self.textField, error: rule.errorMessage()) } } return nil } - } \ No newline at end of file diff --git a/Validator/Validator.swift b/Validator/Validator.swift index 753dd3f..35529a7 100644 --- a/Validator/Validator.swift +++ b/Validator/Validator.swift @@ -11,32 +11,27 @@ import UIKit @objc protocol ValidationDelegate { func validationWasSuccessful() - func validationFailed(errors:[UITextField:ValidationError]) + func validationFailed(errors: [UITextField:ValidationError]) } class Validator { // dictionary to handle complex view hierarchies like dynamic tableview cells - var errors:[UITextField:ValidationError] = [:] - var validations:[UITextField:ValidationRule] = [:] + var errors: [UITextField:ValidationError] = [:] + var validations: [UITextField:ValidationRule] = [:] init(){} // MARK: Using Keys - func registerField(textField:UITextField, rules:[Rule]) { - validations[textField] = ValidationRule(textField: textField, rules: rules, errorLabel: nil) + func registerField(textField: UITextField, errorLabel: UILabel? = nil, rules: [Rule]) { + validations[textField] = ValidationRule(textField: textField, rules: rules, errorLabel: errorLabel) } - func registerField(textField:UITextField, errorLabel:UILabel, rules:[Rule]) { - validations[textField] = ValidationRule(textField: textField, rules:rules, errorLabel:errorLabel) - } - - func validateAll(delegate:ValidationDelegate) { - + func validateAll(delegate: ValidationDelegate) { for field in validations.keys { - if let currentRule:ValidationRule = validations[field] { - if var error:ValidationError = currentRule.validateField() { - if (currentRule.errorLabel != nil) { + if let currentRule: ValidationRule = validations[field] { + if var error: ValidationError = currentRule.validateField() { + if currentRule.errorLabel != nil { error.errorLabel = currentRule.errorLabel } errors[field] = error @@ -53,8 +48,7 @@ class Validator { } } - func clearErrors(){ + func clearErrors() { self.errors = [:] } - } \ No newline at end of file diff --git a/Validator/ViewController.swift b/Validator/ViewController.swift index 1868f0d..61d671b 100644 --- a/Validator/ViewController.swift +++ b/Validator/ViewController.swift @@ -41,12 +41,6 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - - } - @IBAction func submitTapped(sender: AnyObject) { println("Validating...") self.clearErrors() @@ -101,6 +95,4 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate func hideKeyboard(){ self.view.endEditing(true) } - -} - +} \ No newline at end of file diff --git a/Validator/ZipCodeRule.swift b/Validator/ZipCodeRule.swift index 618d3cd..6293f2a 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -15,7 +15,8 @@ class ZipCodeRule: Rule { init(){ self.REGEX = "\\d{5}" } - init(regex:String){ + + init(regex: String){ self.REGEX = regex } @@ -26,5 +27,4 @@ class ZipCodeRule: Rule { func errorMessage() -> String { return "Enter a valid 5 digit zipcode" } - } \ No newline at end of file From a0fa3baf5ab86e30e166a0d2e452f837f9149031 Mon Sep 17 00:00:00 2001 From: Byron Mackay Date: Fri, 3 Apr 2015 14:27:35 -0600 Subject: [PATCH 2/3] Copyright fixes --- Validator/EmailRule.swift | 2 +- Validator/FullNameRule.swift | 2 +- Validator/PasswordRule.swift | 2 +- Validator/RequiredRule.swift | 2 +- Validator/Rule.swift | 2 +- Validator/ValidationError.swift | 2 +- Validator/ValidationRule.swift | 2 +- Validator/Validator.swift | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Validator/EmailRule.swift b/Validator/EmailRule.swift index 5c767fc..f14fef3 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/11/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/FullNameRule.swift b/Validator/FullNameRule.swift index 18b3a82..429c7d4 100644 --- a/Validator/FullNameRule.swift +++ b/Validator/FullNameRule.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/19/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index f1e0c44..fe7d773 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/13/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/RequiredRule.swift b/Validator/RequiredRule.swift index 31aadb5..3e261d3 100644 --- a/Validator/RequiredRule.swift +++ b/Validator/RequiredRule.swift @@ -3,7 +3,7 @@ // pyur-ios // // Created by Jeff Potter on 12/22/14. -// Copyright (c) 2014 ringseven. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/Rule.swift b/Validator/Rule.swift index dfe5b0e..478dcd6 100644 --- a/Validator/Rule.swift +++ b/Validator/Rule.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/11/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/ValidationError.swift b/Validator/ValidationError.swift index f84abe6..cb92993 100644 --- a/Validator/ValidationError.swift +++ b/Validator/ValidationError.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/11/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/ValidationRule.swift b/Validator/ValidationRule.swift index d6d772b..3bd0f23 100644 --- a/Validator/ValidationRule.swift +++ b/Validator/ValidationRule.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/11/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation diff --git a/Validator/Validator.swift b/Validator/Validator.swift index 35529a7..0705bc5 100644 --- a/Validator/Validator.swift +++ b/Validator/Validator.swift @@ -3,7 +3,7 @@ // Pingo // // Created by Jeff Potter on 11/10/14. -// Copyright (c) 2014 Byron Mackay. All rights reserved. +// Copyright (c) 2015 jpotts18. All rights reserved. // import Foundation From 1303eedce26dc8fcdccbbde87d084b4f278a33f9 Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Sat, 4 Apr 2015 00:08:31 -0600 Subject: [PATCH 3/3] refactoring and adding regexrule as super class --- Validator.xcodeproj/project.pbxproj | 4 ++++ Validator/EmailRule.swift | 22 +++++++------------- Validator/FullNameRule.swift | 2 +- Validator/MinLengthRule.swift | 2 +- Validator/PasswordRule.swift | 32 ++++++++++------------------- Validator/RegexRule.swift | 22 ++++++++++++++++++++ Validator/RequiredRule.swift | 2 +- Validator/ZipCodeRule.swift | 24 +++++++--------------- 8 files changed, 54 insertions(+), 56 deletions(-) diff --git a/Validator.xcodeproj/project.pbxproj b/Validator.xcodeproj/project.pbxproj index 2590935..61bb899 100644 --- a/Validator.xcodeproj/project.pbxproj +++ b/Validator.xcodeproj/project.pbxproj @@ -24,6 +24,7 @@ 62DC8D6D1AAA42CE0095DFA7 /* RequiredRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6A1AAA42CE0095DFA7 /* RequiredRule.swift */; }; 62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */; }; 62DC8D711AAA43110095DFA7 /* ZipCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */; }; + 62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E9E2AC1ACFB336000A939C /* RegexRule.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -58,6 +59,7 @@ 62DC8D6A1AAA42CE0095DFA7 /* RequiredRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequiredRule.swift; sourceTree = ""; }; 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRule.swift; sourceTree = ""; }; 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = ""; }; + 62E9E2AC1ACFB336000A939C /* RegexRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexRule.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -165,6 +167,7 @@ 62DC8D671AAA42920095DFA7 /* FullNameRule.swift */, 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */, 628637271AAA49E300BC8FCF /* ConfirmRule.swift */, + 62E9E2AC1ACFB336000A939C /* RegexRule.swift */, ); name = Rules; sourceTree = ""; @@ -270,6 +273,7 @@ buildActionMask = 2147483647; files = ( 62DC8D681AAA42920095DFA7 /* FullNameRule.swift in Sources */, + 62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */, 62D1AE4F1A1E6FF800E4DFF8 /* ValidationError.swift in Sources */, 62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */, 62DC8D6C1AAA42CE0095DFA7 /* EmailRule.swift in Sources */, diff --git a/Validator/EmailRule.swift b/Validator/EmailRule.swift index ad39c87..0223f2a 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -8,25 +8,17 @@ import Foundation -public class EmailRule: Rule { +public class EmailRule: RegexRule { - private let REGEX: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" + public init(){ + super.init(regex: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}") + } - init(){} - - public init(regex:String){ - REGEX = regex + override public init(regex:String){ + super.init(regex: regex) } - public func validate(value: String) -> Bool { - if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) { - if test.evaluateWithObject(value) { - return true - } - } - return false - } - public func errorMessage() -> String { + override public func errorMessage() -> String { return "Must be a valid email address" } } \ No newline at end of file diff --git a/Validator/FullNameRule.swift b/Validator/FullNameRule.swift index 505a66c..acc26b3 100644 --- a/Validator/FullNameRule.swift +++ b/Validator/FullNameRule.swift @@ -15,7 +15,7 @@ public class FullNameRule : Rule { return "Please provide a first & last name" } - func validate(value: String) -> Bool { + public func validate(value: String) -> Bool { var nameArray: [String] = split(value) { $0 == " " } return nameArray.count >= 2 } diff --git a/Validator/MinLengthRule.swift b/Validator/MinLengthRule.swift index ba1bbc9..1ea7f7f 100644 --- a/Validator/MinLengthRule.swift +++ b/Validator/MinLengthRule.swift @@ -10,7 +10,7 @@ import Foundation class MinLengthRule: Rule { - private let DEFAULT_MIN_LENGTH: Int = 3 + private var DEFAULT_MIN_LENGTH: Int = 3 init(){} diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index 5dcc849..baf98eb 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -8,36 +8,26 @@ import Foundation -public class PasswordRule : Rule { - +public class PasswordRule : RegexRule { + // Alternative Regexes - + // 8 characters. One uppercase. One Lowercase. One number. // var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).{8,}$" // // no length. One uppercase. One lowercae. One number. // var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$" - // 8 characters. one uppercase - - private let REGEX: String = "^(?=.*?[A-Z]).{8,}$" - - init(){} - - public init(regex:String){ - self.REGEX = regex - } - - public func validate(value: String) -> Bool { - if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) { - if test.evaluateWithObject(value) { - return true - } - } - return false + public init(){ + super.init(regex: "^(?=.*?[A-Z]).{8,}$") } - public func errorMessage() -> String { + override public init(regex: String) { + super.init(regex: regex) + } + + override public func errorMessage() -> String { return "Must be 8 characters with 1 uppercase" } + } \ No newline at end of file diff --git a/Validator/RegexRule.swift b/Validator/RegexRule.swift index e4ce747..b1b52a5 100644 --- a/Validator/RegexRule.swift +++ b/Validator/RegexRule.swift @@ -7,3 +7,25 @@ // import Foundation + +public class RegexRule : Rule { + + private var REGEX: String = "^(?=.*?[A-Z]).{8,}$" + + init(regex: String){ + self.REGEX = regex + } + + public func validate(value: String) -> Bool { + if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) { + if test.evaluateWithObject(value) { + return true + } + } + return false + } + + public func errorMessage() -> String { + return "Invalid Regular Expression" + } +} diff --git a/Validator/RequiredRule.swift b/Validator/RequiredRule.swift index 0452c68..50b2741 100644 --- a/Validator/RequiredRule.swift +++ b/Validator/RequiredRule.swift @@ -16,7 +16,7 @@ public class RequiredRule: Rule { return "This field is required" } - func validate(value: String) -> Bool { + public func validate(value: String) -> Bool { return !value.isEmpty } diff --git a/Validator/ZipCodeRule.swift b/Validator/ZipCodeRule.swift index beb4105..0df941f 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -8,28 +8,18 @@ import Foundation -public class ZipCodeRule: Rule { - - private let REGEX: String +public class ZipCodeRule: RegexRule { - init(){ - self.REGEX = "\\d{5}" + public init(){ + super.init(regex: "\\d{5}") } - init(regex: String){ - self.REGEX = regex - } - - public func validate(value: String) -> Bool { - if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) { - if test.evaluateWithObject(value) { - return true - } - } - return false + override public init(regex: String) { + super.init(regex: regex) } - public func errorMessage() -> String { + public override func errorMessage() -> String { return "Enter a valid 5 digit zipcode" } + } \ No newline at end of file