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/ConfirmRule.swift b/Validator/ConfirmRule.swift index 61d27b9..94005df 100644 --- a/Validator/ConfirmRule.swift +++ b/Validator/ConfirmRule.swift @@ -18,15 +18,10 @@ public class ConfirmationRule: Rule { } public func validate(value: String) -> Bool { - if self.confirmField.text == value { - return true - } else { - return false - } + return confirmField.text == value } public 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 2f4871d..0223f2a 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -3,32 +3,22 @@ // 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 -public class EmailRule: Rule { - - private let REGEX: String +public class EmailRule: RegexRule { public init(){ - self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" + super.init(regex: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}") } - 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 a29423d..acc26b3 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 @@ -15,13 +15,9 @@ public class FullNameRule : Rule { return "Please provide a first & last name" } - public func validate(value:String) -> Bool { - - var nameArray:[String] = split(value) { $0 == " " } - if nameArray.count >= 2 { - return true - } - return false + public func validate(value: String) -> Bool { + var nameArray: [String] = split(value) { $0 == " " } + return nameArray.count >= 2 } public func errorMessage() -> String { return message diff --git a/Validator/MinLengthRule.swift b/Validator/MinLengthRule.swift index 4971000..1ea7f7f 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 var 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 d08a086..baf98eb 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -3,43 +3,31 @@ // 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 -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 - public init(){ - self.REGEX = "^(?=.*?[A-Z]).{8,}$" + super.init(regex: "^(?=.*?[A-Z]).{8,}$") } - 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 + override public init(regex: String) { + super.init(regex: regex) } - public func errorMessage() -> String { + 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 new file mode 100644 index 0000000..b1b52a5 --- /dev/null +++ b/Validator/RegexRule.swift @@ -0,0 +1,31 @@ +// +// RegexRule.swift +// Validator +// +// Created by Jeff Potter on 4/3/15. +// Copyright (c) 2015 jpotts18. All rights reserved. +// + +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 361a592..50b2741 100644 --- a/Validator/RequiredRule.swift +++ b/Validator/RequiredRule.swift @@ -3,12 +3,11 @@ // 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 - public class RequiredRule: Rule { public init(){} @@ -17,11 +16,8 @@ public class RequiredRule: Rule { return "This field is required" } - public func validate(value:String) -> Bool { - if value.isEmpty { - return false - } - return true + public func validate(value: String) -> Bool { + return !value.isEmpty } public func errorMessage() -> String { diff --git a/Validator/Rule.swift b/Validator/Rule.swift index caed81f..b2559bc 100644 --- a/Validator/Rule.swift +++ b/Validator/Rule.swift @@ -3,12 +3,12 @@ // 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 public 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 49fbace..0938491 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 @@ -18,5 +18,4 @@ public class ValidationError { self.textField = textField self.errorMessage = error } - } \ No newline at end of file diff --git a/Validator/ValidationRule.swift b/Validator/ValidationRule.swift index 48b3279..6433d47 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 @@ -22,12 +22,10 @@ public class ValidationRule { public 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 181b978..5cb196f 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 @@ -11,7 +11,7 @@ import UIKit @objc public protocol ValidationDelegate { func validationWasSuccessful() - func validationFailed(errors:[UITextField:ValidationError]) + func validationFailed(errors: [UITextField:ValidationError]) } public class Validator { @@ -34,9 +34,9 @@ public class Validator { public 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 +53,7 @@ public 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 76df24e..0df941f 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -8,27 +8,17 @@ import Foundation -public class ZipCodeRule: Rule { - - private let REGEX: String +public class ZipCodeRule: RegexRule { - init(){ - self.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 + public init(){ + super.init(regex: "\\d{5}") } - public func errorMessage() -> String { + override public init(regex: String) { + super.init(regex: regex) + } + + public override func errorMessage() -> String { return "Enter a valid 5 digit zipcode" }