Skip to content

Commit 00e8db0

Browse files
committed
refactoring validate methods for regex rules
1 parent aad6604 commit 00e8db0

File tree

3 files changed

+5
-16
lines changed

3 files changed

+5
-16
lines changed

Validator/EmailRule.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
class EmailRule: Rule {
1212

13-
var REGEX : String
13+
private let REGEX: String
1414

1515
init(){
1616
self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
@@ -21,11 +21,7 @@ class EmailRule: Rule {
2121
}
2222

2323
func validate(value: String) -> Bool {
24-
let test = NSPredicate(format: "SELF MATCHES \(self.REGEX)")
25-
if test.evaluateWithObject(value) {
26-
return true
27-
}
28-
return false
24+
return NSPredicate(format: "SELF MATCHES %@", self.REGEX).evaluateWithObject(value)
2925
}
3026

3127
func errorMessage() -> String {

Validator/PasswordRule.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ class PasswordRule : Rule {
3131
}
3232

3333
func validate(value: String) -> Bool {
34-
let test = NSPredicate(format: "SELF MATCHES \(self.REGEX)")
35-
if test.evaluateWithObject(value) {
36-
return true
37-
}
38-
return false
34+
return NSPredicate(format: "SELF MATCHES %@", self.REGEX).evaluateWithObject(value)
3935
}
4036

4137
func errorMessage() -> String {

Validator/ZipCodeRule.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import Foundation
1010

1111
class ZipCodeRule: Rule {
12+
1213
private let REGEX: String
1314

1415
init(){
@@ -19,11 +20,7 @@ class ZipCodeRule: Rule {
1920
}
2021

2122
func validate(value: String) -> Bool {
22-
let test = NSPredicate(format: "SELF MATCHES \(self.REGEX)")
23-
if test.evaluateWithObject(value) {
24-
return true
25-
}
26-
return false
23+
return NSPredicate(format: "SELF MATCHES %@", self.REGEX).evaluateWithObject(value)
2724
}
2825

2926
func errorMessage() -> String {

0 commit comments

Comments
 (0)