From ba9615cc8573ea9c410ad5c51cbe3bc6e66fd68a Mon Sep 17 00:00:00 2001 From: mla2c Date: Mon, 1 Jun 2015 14:13:26 +0200 Subject: [PATCH 1/3] Changes from leodasvacas fork (Custom messages for regex rules, commit 157e9791fb1774f31636321cc6c3cff46b32d92c) --- Validator/EmailRule.swift | 12 +++--------- Validator/PasswordRule.swift | 19 ++++++------------- Validator/RegexRule.swift | 6 ++++-- Validator/ZipCodeRule.swift | 13 ++----------- 4 files changed, 15 insertions(+), 35 deletions(-) diff --git a/Validator/EmailRule.swift b/Validator/EmailRule.swift index 0223f2a..50b83d4 100644 --- a/Validator/EmailRule.swift +++ b/Validator/EmailRule.swift @@ -10,15 +10,9 @@ import Foundation public class EmailRule: RegexRule { - public init(){ - super.init(regex: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}") - } + static let 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" + 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..772f50a 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]).*?$" - public init(){ - super.init(regex: "^(?=.*?[A-Z]).{8,}$") + static let 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..42f6fb5 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,}$" + private var message : String - public init(regex: 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..52a5f7a 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 From 0edfb58cc442d89fe6776b2e3cbfd8795e22b515 Mon Sep 17 00:00:00 2001 From: mla2c Date: Mon, 1 Jun 2015 14:54:50 +0200 Subject: [PATCH 2/3] All rules: init with optional user-defined message possible --- Validator/ConfirmRule.swift | 6 ++++-- Validator/FloatRule.swift | 8 +++++--- Validator/FullNameRule.swift | 11 ++++++----- Validator/MaxLengthRule.swift | 8 +++++--- Validator/MinLengthRule.swift | 8 +++++--- Validator/RequiredRule.swift | 6 +++--- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Validator/ConfirmRule.swift b/Validator/ConfirmRule.swift index 46ff215..403d0bf 100644 --- a/Validator/ConfirmRule.swift +++ b/Validator/ConfirmRule.swift @@ -12,9 +12,11 @@ import UIKit public class ConfirmationRule: Rule { private let confirmField: UITextField + private var message : String - public init(confirmField: UITextField) { + public init(confirmField: UITextField, message : String = "This field does not match"){ self.confirmField = confirmField + self.message = message } public func validate(value: String) -> Bool { @@ -22,6 +24,6 @@ public class ConfirmationRule: Rule { } public func errorMessage() -> String { - return "This field does not match" + return message } } \ No newline at end of file diff --git a/Validator/FloatRule.swift b/Validator/FloatRule.swift index 4bf1c28..8e5c1f8 100644 --- a/Validator/FloatRule.swift +++ b/Validator/FloatRule.swift @@ -10,8 +10,10 @@ import Foundation public class FloatRule:Rule { - public init(){ - + private var message : String + + public init(message : String = "This must be a number with or without a decimal"){ + self.message = message } public func validate(value: String) -> Bool { @@ -24,6 +26,6 @@ public class FloatRule:Rule { } public func errorMessage() -> String { - return "This must be a number with or without a decimal" + return message } } \ No newline at end of file diff --git a/Validator/FullNameRule.swift b/Validator/FullNameRule.swift index a030214..9c79f53 100644 --- a/Validator/FullNameRule.swift +++ b/Validator/FullNameRule.swift @@ -11,16 +11,17 @@ import Foundation public class FullNameRule : Rule { - var message:String { - return "Please provide a first & last name" + private var message : String + + public init(message : String = "Please provide a first & last name"){ + self.message = message } - - public init(){} - + 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/MaxLengthRule.swift b/Validator/MaxLengthRule.swift index f6b346a..fd29af1 100644 --- a/Validator/MaxLengthRule.swift +++ b/Validator/MaxLengthRule.swift @@ -10,18 +10,20 @@ import Foundation public class MaxLengthRule: Rule { private var DEFAULT_LENGTH: Int = 16 + private var message : String = "Must be at most 16 characters long" public init(){} - public init(length: Int){ + public init(length: Int, message : String = "Must be at most %ld characters long"){ self.DEFAULT_LENGTH = length + self.message = NSString(format: message, self.DEFAULT_LENGTH) as String } - + public func validate(value: String) -> Bool { return count(value) <= DEFAULT_LENGTH } public func errorMessage() -> String { - return "Must be at most \(DEFAULT_LENGTH) characters long" + return message } } diff --git a/Validator/MinLengthRule.swift b/Validator/MinLengthRule.swift index c5cf308..3ff63ea 100644 --- a/Validator/MinLengthRule.swift +++ b/Validator/MinLengthRule.swift @@ -11,11 +11,13 @@ import Foundation public class MinLengthRule: Rule { private var DEFAULT_LENGTH: Int = 3 - + private var message : String = "Must be at least 3 characters long" + public init(){} - public init(length: Int){ + public init(length: Int, message : String = "Must be at least %ld characters long"){ self.DEFAULT_LENGTH = length + self.message = NSString(format: message, self.DEFAULT_LENGTH) as String } public func validate(value: String) -> Bool { @@ -23,6 +25,6 @@ public class MinLengthRule: Rule { } public func errorMessage() -> String { - return "Must be at least \(DEFAULT_LENGTH) characters long" + return message } } diff --git a/Validator/RequiredRule.swift b/Validator/RequiredRule.swift index 50b2741..14e8e63 100644 --- a/Validator/RequiredRule.swift +++ b/Validator/RequiredRule.swift @@ -10,10 +10,10 @@ import Foundation public class RequiredRule: Rule { - public init(){} + private var message : String - var message: String { - return "This field is required" + public init(message : String = "This field is required"){ + self.message = message } public func validate(value: String) -> Bool { From 4d3f245f7acd42296ca74aa6b8f8f69966c003bc Mon Sep 17 00:00:00 2001 From: mla2c Date: Fri, 12 Jun 2015 08:43:19 +0200 Subject: [PATCH 3/3] Correct conflict --- Validator/PasswordRule.swift | 7 ------- Validator/RegexRule.swift | 7 ------- Validator/ZipCodeRule.swift | 5 ----- 3 files changed, 19 deletions(-) diff --git a/Validator/PasswordRule.swift b/Validator/PasswordRule.swift index fdaea8e..823e402 100644 --- a/Validator/PasswordRule.swift +++ b/Validator/PasswordRule.swift @@ -17,18 +17,11 @@ public class PasswordRule : RegexRule { // // no length. One uppercase. One lowercae. One number. // static let regex = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$" -<<<<<<< HEAD static let regex = "^(?=.*?[A-Z]).{8,}$" public convenience init(message : String = "Must be 8 characters with 1 uppercase") { self.init(regex: PasswordRule.regex, message : message) -======= - static let regex = "^(?=.*?[A-Z]).{8,}$" - - public convenience init(message : String = "Must be 8 characters with 1 uppercase") { - self.init(regex: PasswordRule.regex, message : message) ->>>>>>> upstream/master } } \ No newline at end of file diff --git a/Validator/RegexRule.swift b/Validator/RegexRule.swift index 64ac49b..42f6fb5 100644 --- a/Validator/RegexRule.swift +++ b/Validator/RegexRule.swift @@ -12,17 +12,10 @@ public class RegexRule : Rule { private var REGEX: String = "^(?=.*?[A-Z]).{8,}$" private var message : String -<<<<<<< HEAD public init(regex: String, message: String = "Invalid Regular Expression"){ self.REGEX = regex self.message = message -======= - - public init(regex: String, message: String = "Invalid Regular Expression"){ - self.REGEX = regex - self.message = message ->>>>>>> upstream/master } public func validate(value: String) -> Bool { diff --git a/Validator/ZipCodeRule.swift b/Validator/ZipCodeRule.swift index daf2c6a..52a5f7a 100644 --- a/Validator/ZipCodeRule.swift +++ b/Validator/ZipCodeRule.swift @@ -10,12 +10,7 @@ import Foundation public class ZipCodeRule: RegexRule { -<<<<<<< HEAD public convenience init(message : String = "Enter a valid 5 digit zipcode"){ self.init(regex: "\\d{5}", message : message) -======= - public convenience init(message : String = "Enter a valid 5 digit zipcode"){ - self.init(regex: "\\d{5}", message : message) ->>>>>>> upstream/master } } \ No newline at end of file