Skip to content

Commit

Permalink
Merge pull request #39 from KVTaniguchi/RefactorValidator
Browse files Browse the repository at this point in the history
refactor Validator
  • Loading branch information
jpotts18 committed Jul 21, 2015
2 parents f5376fb + 21a5cc5 commit dc9224c
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions Validator/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,32 @@ import UIKit

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

public init(){}

// MARK: Private functions

private func clearErrors() {
self.errors = [:]
}

private func validateAllFields() {

self.clearErrors()
errors = [:]

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

// let the user transform the field if they want
if let transform = self.errorStyleTransform {
transform(validationError: error)
}
} else {
// No error
// let the user transform the field if they want
if let transform = self.successStyleTransform {
transform(validationRule: currentRule)
}
for (textField, rule) in validations {
if var error = rule.validateField() {
errors[textField] = error

// let the user transform the field if they want
if let transform = self.errorStyleTransform {
transform(validationError: error)
}
} else {
// No error
// let the user transform the field if they want
if let transform = self.successStyleTransform {
transform(validationRule: rule)
}
}
}
Expand Down

0 comments on commit dc9224c

Please sign in to comment.