From 157e9791fb1774f31636321cc6c3cff46b32d92c Mon Sep 17 00:00:00 2001 From: "leonardo.yvens" Date: Sun, 24 May 2015 13:12:23 -0300 Subject: [PATCH] Custom messages for regex rules --- .DS_Store | Bin 0 -> 6148 bytes Validator/EmailRule.swift | 18 ++++++------------ Validator/PasswordRule.swift | 19 ++++++------------- Validator/RegexRule.swift | 8 +++++--- Validator/ZipCodeRule.swift | 13 ++----------- 5 files changed, 19 insertions(+), 39 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1713d9fe0a3e4a6c598f3625bf8c2a7be56ce9dc GIT binary patch literal 6148 zcmeHK%TLrm7@rSNwya8dDGyDmTr_aNEQWY6DGNS`l3iW0fIQZAJ1i4QXVVVvknGLJ zP48ZO`!^U96Ql88@L>ECym;Z}H|;DB!K)I}FPZs$^S#>nP21@ZLLj~5TqHy%ge0&@ ztZTsT0Fi##_DPMh)FMJy!yy(CgcILh*%uufVg_Oc4jTjV-PK4Q-&>b3vg`c~%C^gG z^lsT@mj$A)?<>^Q)*U%opU@hT$z(%PZ)%vy&GVIz13ZGFqfErd@R@A`HDmCAAeHS^$j-P1Mo0?l%+fJTp@96A0eWtV1 zDCjM=Ra_|fa#};&<$l^-qEjWGIk~b`WL{}P&{dIR>$IRZ{}y_OIMrI&s{gm111>vCZ_{jx)DJaN4z z+p(C|htR0$ux*@mEJ1Hsfyk7pM;%S;HnS=-L3+p_8O29>fvl6qRuo7YT>A)^L0wC6*ITY08SwiL*3QdKT z2+@NAQ7U4T3hIafQ99ZU6=y1}L>Q$LP=^mtR|a)Lf$-{B-w4AAm=VT(%s|Y*UIuET zp)U9TtA9TK?@fwFF#|CJ{}lsJH=G+zRU!;iuv6P{sK~9j e9a|{qGo*rODy&3^7L@o$AfzGgVg~*y1HS-5j_l?D literal 0 HcmV?d00001 diff --git a/Validator/EmailRule.swift b/Validator/EmailRule.swift index 0223f2a..03ab470 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -9,16 +9,10 @@ import Foundation public class EmailRule: RegexRule { - - public init(){ - super.init(regex: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}") - } - - override public init(regex:String){ - super.init(regex: regex) - } - - override public func errorMessage() -> String { - return "Must be a valid email address" - } + + static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" + + public convenience init(message : String = "Must be a valid email address"){ + self.init(regex: EmailRule.regex, message: message) + } } \ No newline at end of file diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index baf98eb..d64af34 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -13,21 +13,14 @@ public class PasswordRule : RegexRule { // Alternative Regexes // 8 characters. One uppercase. One Lowercase. One number. - // var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).{8,}$" + // static let 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]).*?$" + // static let regex = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$" + + static let regex = "^(?=.*?[A-Z]).{8,}$" - public init(){ - super.init(regex: "^(?=.*?[A-Z]).{8,}$") + public convenience init(message : String = "Must be 8 characters with 1 uppercase") { + self.init(regex: PasswordRule.regex, message : message) } - - 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 af00238..a22123e 100644 --- a/Validator/RegexRule.swift +++ b/Validator/RegexRule.swift @@ -11,9 +11,11 @@ import Foundation public class RegexRule : Rule { private var REGEX: String = "^(?=.*?[A-Z]).{8,}$" - - public init(regex: String){ + private var message : String + + public init(regex: String, message: String = "Invalid Regular Expression"){ self.REGEX = regex + self.message = message } public func validate(value: String) -> Bool { @@ -22,6 +24,6 @@ public class RegexRule : Rule { } public func errorMessage() -> String { - return "Invalid Regular Expression" + return message } } diff --git a/Validator/ZipCodeRule.swift b/Validator/ZipCodeRule.swift index 0df941f..360ccce 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -10,16 +10,7 @@ import Foundation public class ZipCodeRule: RegexRule { - public init(){ - super.init(regex: "\\d{5}") + public convenience init(message : String = "Enter a valid 5 digit zipcode"){ + self.init(regex: "\\d{5}", message : message) } - - override public init(regex: String) { - super.init(regex: regex) - } - - public override func errorMessage() -> String { - return "Enter a valid 5 digit zipcode" - } - } \ No newline at end of file