diff --git a/Validator/Validator.swift b/Validator/Validator.swift index 458a605..28eed19 100644 --- a/Validator/Validator.swift +++ b/Validator/Validator.swift @@ -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 = [:] } diff --git a/ValidatorTests/ValidatorTests.swift b/ValidatorTests/ValidatorTests.swift index 854e191..95c9095 100644 --- a/ValidatorTests/ValidatorTests.swift +++ b/ValidatorTests/ValidatorTests.swift @@ -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") + } + } }