Skip to content

Commit 34eced2

Browse files
committed
Merge pull request #26 from cameronmccord2/master
Added validate function with callback + tests
2 parents 8d25fed + dfa5c25 commit 34eced2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Validator/Validator.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ public class Validator {
5757
}
5858
}
5959

60+
public func validate(callback:(errors:[UITextField:ValidationError])->Void) -> Void {
61+
62+
for field in validations.keys {
63+
if let currentRule:ValidationRule = validations[field] {
64+
if var error:ValidationError = currentRule.validateField() {
65+
errors[field] = error
66+
} else {
67+
errors.removeValueForKey(field)
68+
}
69+
}
70+
}
71+
72+
callback(errors: errors)
73+
}
74+
6075
func clearErrors() {
6176
self.errors = [:]
6277
}

ValidatorTests/ValidatorTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,18 @@ class ValidatorTests: XCTestCase {
164164
UNREGISTER_VALIDATOR.unregisterField(UNREGISTER_TXT_FIELD)
165165
XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister")
166166
}
167+
168+
// MARK: Validate Functions
169+
170+
func testValidateWithCallback() {
171+
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: [EmailRule()])
172+
REGISTER_TXT_FIELD.text = VALID_EMAIL
173+
REGISTER_VALIDATOR.validate { (errors) -> Void in
174+
XCTAssert(errors.count == 0, "Should not come back with errors")
175+
}
176+
REGISTER_TXT_FIELD.text = INVALID_EMAIL
177+
REGISTER_VALIDATOR.validate { (errors) -> Void in
178+
XCTAssert(errors.count == 1, "Should come back with 1 error")
179+
}
180+
}
167181
}

0 commit comments

Comments
 (0)