refactoring validate methods for regex rules

This commit is contained in:
Jeff Potter 2015-03-30 15:59:40 -06:00
parent aad6604eac
commit 00e8db0ce2
3 changed files with 5 additions and 16 deletions

View File

@ -10,7 +10,7 @@ import Foundation
class EmailRule: Rule {
var REGEX : String
private let REGEX: String
init(){
self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
@ -21,11 +21,7 @@ class EmailRule: Rule {
}
func validate(value: String) -> Bool {
let test = NSPredicate(format: "SELF MATCHES \(self.REGEX)")
if test.evaluateWithObject(value) {
return true
}
return false
return NSPredicate(format: "SELF MATCHES %@", self.REGEX).evaluateWithObject(value)
}
func errorMessage() -> String {

View File

@ -31,11 +31,7 @@ class PasswordRule : Rule {
}
func validate(value: String) -> Bool {
let test = NSPredicate(format: "SELF MATCHES \(self.REGEX)")
if test.evaluateWithObject(value) {
return true
}
return false
return NSPredicate(format: "SELF MATCHES %@", self.REGEX).evaluateWithObject(value)
}
func errorMessage() -> String {

View File

@ -9,6 +9,7 @@
import Foundation
class ZipCodeRule: Rule {
private let REGEX: String
init(){
@ -19,11 +20,7 @@ class ZipCodeRule: Rule {
}
func validate(value: String) -> Bool {
let test = NSPredicate(format: "SELF MATCHES \(self.REGEX)")
if test.evaluateWithObject(value) {
return true
}
return false
return NSPredicate(format: "SELF MATCHES %@", self.REGEX).evaluateWithObject(value)
}
func errorMessage() -> String {