Refactor dictionary to return field itself instead of ObjectIdentifier
This commit is contained in:
parent
8f116df330
commit
2fabe418ea
|
|
@ -14,11 +14,11 @@ import UIKit
|
|||
*/
|
||||
public class Validator {
|
||||
/// Dictionary to hold all fields (and accompanying rules) that will undergo validation.
|
||||
public var validations = [ObjectIdentifier:ValidationRule]()
|
||||
public var validations = ValidatorDictionary<ValidationRule>()
|
||||
/// Dictionary to hold fields (and accompanying errors) that were unsuccessfully validated.
|
||||
public var errors = [ObjectIdentifier:ValidationError]()
|
||||
public var errors = ValidatorDictionary<ValidationError>()
|
||||
/// Dictionary to hold fields by their object identifiers
|
||||
private var fields = [ObjectIdentifier:Validatable]()
|
||||
private var fields = ValidatorDictionary<Validatable>()
|
||||
/// Variable that holds success closure to display positive status of field.
|
||||
private var successStyleTransform:((validationRule:ValidationRule)->Void)?
|
||||
/// Variable that holds error closure to display negative status of field.
|
||||
|
|
@ -36,11 +36,11 @@ public class Validator {
|
|||
*/
|
||||
private func validateAllFields() {
|
||||
|
||||
errors = [:]
|
||||
errors = ValidatorDictionary<ValidationError>()
|
||||
|
||||
for (field, rule) in validations {
|
||||
for (_, rule) in validations {
|
||||
if let error = rule.validateField() {
|
||||
errors[field] = error
|
||||
errors[rule.field] = error
|
||||
|
||||
// let the user transform the field if they want
|
||||
if let transform = self.errorStyleTransform {
|
||||
|
|
@ -66,10 +66,9 @@ public class Validator {
|
|||
- returns: No return value.
|
||||
*/
|
||||
public func validateField(field: ValidatableField, callback: (error:ValidationError?) -> Void){
|
||||
let oid = ObjectIdentifier(field)
|
||||
if let fieldRule = validations[oid] {
|
||||
if let fieldRule = validations[field] {
|
||||
if let error = fieldRule.validateField() {
|
||||
errors[oid] = error
|
||||
errors[field] = error
|
||||
if let transform = self.errorStyleTransform {
|
||||
transform(validationError: error)
|
||||
}
|
||||
|
|
@ -107,9 +106,8 @@ public class Validator {
|
|||
- returns: No return value
|
||||
*/
|
||||
public func registerField(field:ValidatableField, rules:[Rule]) {
|
||||
let oid = ObjectIdentifier(field)
|
||||
validations[oid] = ValidationRule(field: field, rules: rules, errorLabel: nil)
|
||||
fields[oid] = field
|
||||
validations[field] = ValidationRule(field: field, rules: rules, errorLabel: nil)
|
||||
fields[field] = field
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -121,9 +119,8 @@ public class Validator {
|
|||
- returns: No return value
|
||||
*/
|
||||
public func registerField(field: ValidatableField, errorLabel:UILabel, rules:[Rule]) {
|
||||
let oid = ObjectIdentifier(field)
|
||||
validations[oid] = ValidationRule(field: field, rules:rules, errorLabel:errorLabel)
|
||||
fields[oid] = field
|
||||
validations[field] = ValidationRule(field: field, rules:rules, errorLabel:errorLabel)
|
||||
fields[field] = field
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -133,9 +130,8 @@ public class Validator {
|
|||
- returns: No return value
|
||||
*/
|
||||
public func unregisterField(field:ValidatableField) {
|
||||
let oid = ObjectIdentifier(field)
|
||||
validations.removeValueForKey(oid)
|
||||
errors.removeValueForKey(oid)
|
||||
validations.removeValueForKey(field)
|
||||
errors.removeValueForKey(field)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -150,7 +146,7 @@ public class Validator {
|
|||
if errors.isEmpty {
|
||||
delegate.validationSuccessful()
|
||||
} else {
|
||||
delegate.validationFailed(errors.map { (fields[$0]!, $1) })
|
||||
delegate.validationFailed(errors.map { (fields[$1.field]!, $1) })
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -165,6 +161,6 @@ public class Validator {
|
|||
|
||||
self.validateAllFields()
|
||||
|
||||
callback(errors: errors.map { (fields[$0]!, $1) } )
|
||||
callback(errors: errors.map { (fields[$1.field]!, $1) } )
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
//
|
||||
// ValidatorDictionary.swift
|
||||
// Validator
|
||||
//
|
||||
// Created by Deniz Adalar on 04/05/16.
|
||||
// Copyright © 2016 jpotts18. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct ValidatorDictionary<T> : SequenceType {
|
||||
|
||||
private var innerDictionary: [ObjectIdentifier: T] = [:];
|
||||
|
||||
public subscript(key: ValidatableField?) -> T? {
|
||||
get {
|
||||
if let key = key {
|
||||
return innerDictionary[ObjectIdentifier(key)];
|
||||
} else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
set(newValue) {
|
||||
if let key = key {
|
||||
innerDictionary[ObjectIdentifier(key)] = newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func removeAll() {
|
||||
innerDictionary.removeAll()
|
||||
}
|
||||
|
||||
public mutating func removeValueForKey(key: ValidatableField) {
|
||||
innerDictionary.removeValueForKey(ObjectIdentifier(key))
|
||||
}
|
||||
|
||||
public var isEmpty: Bool {
|
||||
return innerDictionary.isEmpty
|
||||
}
|
||||
|
||||
public func generate() -> DictionaryGenerator<ObjectIdentifier ,T> {
|
||||
return innerDictionary.generate()
|
||||
}
|
||||
}
|
||||
|
|
@ -323,13 +323,13 @@ class SwiftValidatorTests: XCTestCase {
|
|||
|
||||
func testRegisterField(){
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: REGISTER_RULES)
|
||||
XCTAssert(REGISTER_VALIDATOR.validations[ObjectIdentifier(REGISTER_TXT_FIELD)] != nil, "Textfield should register")
|
||||
XCTAssert(REGISTER_VALIDATOR.validations[REGISTER_TXT_FIELD] != nil, "Textfield should register")
|
||||
}
|
||||
|
||||
func testUnregisterField(){
|
||||
UNREGISTER_VALIDATOR.registerField(UNREGISTER_TXT_FIELD, rules: UNREGISTER_RULES)
|
||||
UNREGISTER_VALIDATOR.unregisterField(UNREGISTER_TXT_FIELD)
|
||||
XCTAssert(UNREGISTER_VALIDATOR.validations[ObjectIdentifier(UNREGISTER_TXT_FIELD)] == nil, "Textfield should unregister")
|
||||
XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister")
|
||||
}
|
||||
|
||||
func testUnregisterError(){
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@
|
|||
FB465D001B9889EA00398388 /* ValidationError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CF11B9889EA00398388 /* ValidationError.swift */; };
|
||||
FB465D011B9889EA00398388 /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CF21B9889EA00398388 /* Validator.swift */; };
|
||||
FB51E5B01CD208B8004DE696 /* Validatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB51E5AF1CD208B8004DE696 /* Validatable.swift */; };
|
||||
FBA963631CDA10130071F03E /* ValidatorDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA963621CDA10130071F03E /* ValidatorDictionary.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
|
@ -128,6 +129,7 @@
|
|||
FB465CF11B9889EA00398388 /* ValidationError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidationError.swift; sourceTree = "<group>"; };
|
||||
FB465CF21B9889EA00398388 /* Validator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = "<group>"; };
|
||||
FB51E5AF1CD208B8004DE696 /* Validatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validatable.swift; sourceTree = "<group>"; };
|
||||
FBA963621CDA10130071F03E /* ValidatorDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorDictionary.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
|
@ -293,6 +295,7 @@
|
|||
FB465CF11B9889EA00398388 /* ValidationError.swift */,
|
||||
FB465CF21B9889EA00398388 /* Validator.swift */,
|
||||
FB51E5AF1CD208B8004DE696 /* Validatable.swift */,
|
||||
FBA963621CDA10130071F03E /* ValidatorDictionary.swift */,
|
||||
);
|
||||
path = Core;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -490,6 +493,7 @@
|
|||
files = (
|
||||
FB465CF41B9889EA00398388 /* EmailRule.swift in Sources */,
|
||||
FB465CF61B9889EA00398388 /* FullNameRule.swift in Sources */,
|
||||
FBA963631CDA10130071F03E /* ValidatorDictionary.swift in Sources */,
|
||||
FB465CFF1B9889EA00398388 /* ZipCodeRule.swift in Sources */,
|
||||
FB465CF91B9889EA00398388 /* PasswordRule.swift in Sources */,
|
||||
7CC1E4D11C637A7700AF013C /* AlphaNumericRule.swift in Sources */,
|
||||
|
|
|
|||
Loading…
Reference in New Issue