Skip to content

Commit

Permalink
Merge pull request #26 from cameronmccord2/master
Browse files Browse the repository at this point in the history
Added validate function with callback + tests
  • Loading branch information
jpotts18 committed Apr 30, 2015
2 parents 8d25fed + dfa5c25 commit 34eced2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Validator/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ public class Validator {
}
}

public func validate(callback:(errors:[UITextField:ValidationError])->Void) -> Void {

for field in validations.keys {
if let currentRule:ValidationRule = validations[field] {
if var error:ValidationError = currentRule.validateField() {
errors[field] = error
} else {
errors.removeValueForKey(field)
}
}
}

callback(errors: errors)
}

func clearErrors() {
self.errors = [:]
}
Expand Down
14 changes: 14 additions & 0 deletions ValidatorTests/ValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,18 @@ class ValidatorTests: XCTestCase {
UNREGISTER_VALIDATOR.unregisterField(UNREGISTER_TXT_FIELD)
XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister")
}

// MARK: Validate Functions

func testValidateWithCallback() {
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: [EmailRule()])
REGISTER_TXT_FIELD.text = VALID_EMAIL
REGISTER_VALIDATOR.validate { (errors) -> Void in
XCTAssert(errors.count == 0, "Should not come back with errors")
}
REGISTER_TXT_FIELD.text = INVALID_EMAIL
REGISTER_VALIDATOR.validate { (errors) -> Void in
XCTAssert(errors.count == 1, "Should come back with 1 error")
}
}
}

0 comments on commit 34eced2

Please sign in to comment.