refactoring and adding regexrule as super class
This commit is contained in:
parent
9c4ee432c2
commit
1303eedce2
|
|
@ -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 = "<group>"; };
|
||||
62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRule.swift; sourceTree = "<group>"; };
|
||||
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = "<group>"; };
|
||||
62E9E2AC1ACFB336000A939C /* RegexRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexRule.swift; sourceTree = "<group>"; };
|
||||
/* 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 = "<group>";
|
||||
|
|
@ -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 */,
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import Foundation
|
|||
|
||||
class MinLengthRule: Rule {
|
||||
|
||||
private let DEFAULT_MIN_LENGTH: Int = 3
|
||||
private var DEFAULT_MIN_LENGTH: Int = 3
|
||||
|
||||
init(){}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue