Skip to content

Commit 21a5cc5

Browse files
author
Kevin Taniguchi
committed
refactor Validator
using type inferencing loop keys, values in validations
1 parent 724a05c commit 21a5cc5

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

Validator/Validator.swift

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,32 @@ import UIKit
1616

1717
public class Validator {
1818
// dictionary to handle complex view hierarchies like dynamic tableview cells
19-
public var errors:[UITextField:ValidationError] = [:]
20-
public var validations:[UITextField:ValidationRule] = [:]
19+
public var errors = [UITextField:ValidationError]()
20+
public var validations = [UITextField:ValidationRule]()
2121
private var successStyleTransform:((validationRule:ValidationRule)->Void)?
2222
private var errorStyleTransform:((validationError:ValidationError)->Void)?
2323

2424
public init(){}
2525

2626
// MARK: Private functions
2727

28-
private func clearErrors() {
29-
self.errors = [:]
30-
}
31-
3228
private func validateAllFields() {
3329

34-
self.clearErrors()
30+
errors = [:]
3531

36-
for field in validations.keys {
37-
if let currentRule: ValidationRule = validations[field] {
38-
if var error: ValidationError = currentRule.validateField() {
39-
errors[field] = error
40-
41-
// let the user transform the field if they want
42-
if let transform = self.errorStyleTransform {
43-
transform(validationError: error)
44-
}
45-
} else {
46-
// No error
47-
// let the user transform the field if they want
48-
if let transform = self.successStyleTransform {
49-
transform(validationRule: currentRule)
50-
}
32+
for (textField, rule) in validations {
33+
if var error = rule.validateField() {
34+
errors[textField] = error
35+
36+
// let the user transform the field if they want
37+
if let transform = self.errorStyleTransform {
38+
transform(validationError: error)
39+
}
40+
} else {
41+
// No error
42+
// let the user transform the field if they want
43+
if let transform = self.successStyleTransform {
44+
transform(validationRule: rule)
5145
}
5246
}
5347
}

0 commit comments

Comments
 (0)