diff --git a/SwiftValidator/Core/ValidationError.swift b/SwiftValidator/Core/ValidationError.swift index e6f9c07..6b5c009 100644 --- a/SwiftValidator/Core/ValidationError.swift +++ b/SwiftValidator/Core/ValidationError.swift @@ -1,7 +1,4 @@ // -// File.swift -// Pingo -// // Created by Jeff Potter on 11/11/14. // Copyright (c) 2015 jpotts18. All rights reserved. // diff --git a/SwiftValidator/Core/Validator.swift b/SwiftValidator/Core/Validator.swift index a5d41df..3eb1b3d 100644 --- a/SwiftValidator/Core/Validator.swift +++ b/SwiftValidator/Core/Validator.swift @@ -1,6 +1,5 @@ // // Validator.swift -// Pingo // // Created by Jeff Potter on 11/10/14. // Copyright (c) 2015 jpotts18. All rights reserved. @@ -10,8 +9,10 @@ import Foundation import UIKit @objc public protocol ValidationDelegate { +// func validationWillRun() func validationSuccessful() func validationFailed(errors: [UITextField:ValidationError]) +// func validationDidRun() } public class Validator { @@ -89,6 +90,8 @@ public class Validator { public func validate(delegate:ValidationDelegate) { +// delegate.validationWillRun() + self.validateAllFields() if errors.isEmpty { @@ -96,6 +99,8 @@ public class Validator { } else { delegate.validationFailed(errors) } + +// delegate.validationDidRun() } public func validate(callback:(errors:[UITextField:ValidationError])->Void) -> Void { diff --git a/SwiftValidator/Rules/EmailRule.swift b/SwiftValidator/Rules/EmailRule.swift index 98adc08..9871fa5 100644 --- a/SwiftValidator/Rules/EmailRule.swift +++ b/SwiftValidator/Rules/EmailRule.swift @@ -1,6 +1,5 @@ // // EmailValidation.swift -// Pingo // // Created by Jeff Potter on 11/11/14. // Copyright (c) 2015 jpotts18. All rights reserved. @@ -9,7 +8,6 @@ import Foundation public class EmailRule: RegexRule { - static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" diff --git a/SwiftValidator/Rules/ExactLengthRule.swift b/SwiftValidator/Rules/ExactLengthRule.swift new file mode 100644 index 0000000..9b82a7d --- /dev/null +++ b/SwiftValidator/Rules/ExactLengthRule.swift @@ -0,0 +1,27 @@ +// +// ExactLengthRule.swift +// Validator +// +// Created by Jeff Potter on 2/3/16. +// Copyright © 2016 jpotts18. All rights reserved. +// + +import Foundation + +public class ExactLengthRule : Rule { + private var message : String = "Must be at most 16 characters long" + private var length : Int + + public init(length: Int, message : String = "Must be exactly %ld characters long"){ + self.length = length + self.message = NSString(format: message, self.length) as String + } + + public func validate(value: String) -> Bool { + return value.characters.count == length + } + + public func errorMessage() -> String { + return message + } +} \ No newline at end of file diff --git a/SwiftValidator/Rules/FullNameRule.swift b/SwiftValidator/Rules/FullNameRule.swift index f1feb1d..d8e98dc 100644 --- a/SwiftValidator/Rules/FullNameRule.swift +++ b/SwiftValidator/Rules/FullNameRule.swift @@ -1,6 +1,5 @@ // // FullNameValidation.swift -// Pingo // // Created by Jeff Potter on 11/19/14. // Copyright (c) 2015 jpotts18. All rights reserved. diff --git a/SwiftValidator/Rules/PasswordRule.swift b/SwiftValidator/Rules/PasswordRule.swift index 823e402..2f6ecba 100644 --- a/SwiftValidator/Rules/PasswordRule.swift +++ b/SwiftValidator/Rules/PasswordRule.swift @@ -1,6 +1,5 @@ // // PasswordValidation.swift -// Pingo // // Created by Jeff Potter on 11/13/14. // Copyright (c) 2015 jpotts18. All rights reserved. @@ -22,6 +21,5 @@ public class PasswordRule : RegexRule { public convenience init(message : String = "Must be 8 characters with 1 uppercase") { self.init(regex: PasswordRule.regex, message : message) - } } \ No newline at end of file diff --git a/SwiftValidator/Rules/PhoneNumberRule.swift b/SwiftValidator/Rules/PhoneNumberRule.swift index 4c6f524..52f8a5b 100644 --- a/SwiftValidator/Rules/PhoneNumberRule.swift +++ b/SwiftValidator/Rules/PhoneNumberRule.swift @@ -1,6 +1,5 @@ // // PhoneValidation.swift -// Pingo // // Created by Jeff Potter on 11/11/14. // Copyright (c) 2014 Byron Mackay. All rights reserved. @@ -8,21 +7,13 @@ import Foundation -class PhoneNumberRule: Rule { +public class PhoneNumberRule: RegexRule { // let PHONE_REGEX = "^\\d{3}-\\d{3}-\\d{4}$" - let PHONE_REGEX = "^\\d{10}$" + + static let regex = "^\\d{10}$" - var message:String { - return "Enter a valid 10 digit phone number" - } - - func validate(value: String) -> Bool { - let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX) - return phoneTest.evaluateWithObject(value) - } - - func errorMessage() -> String { - return message + public convenience init(message : String = "Enter a valid 10 digit phone number") { + self.init(regex: PhoneNumberRule.regex, message : message) } } diff --git a/SwiftValidator/Rules/Rule.swift b/SwiftValidator/Rules/Rule.swift index b2559bc..506337e 100644 --- a/SwiftValidator/Rules/Rule.swift +++ b/SwiftValidator/Rules/Rule.swift @@ -1,6 +1,5 @@ // // Validation.swift -// Pingo // // Created by Jeff Potter on 11/11/14. // Copyright (c) 2015 jpotts18. All rights reserved. diff --git a/SwiftValidator/Rules/ValidationRule.swift b/SwiftValidator/Rules/ValidationRule.swift index c91d209..a1f0723 100644 --- a/SwiftValidator/Rules/ValidationRule.swift +++ b/SwiftValidator/Rules/ValidationRule.swift @@ -1,6 +1,5 @@ // // ValidationRule.swift -// Pingo // // Created by Jeff Potter on 11/11/14. // Copyright (c) 2015 jpotts18. All rights reserved. diff --git a/SwiftValidatorTests/SwiftValidatorTests.swift b/SwiftValidatorTests/SwiftValidatorTests.swift index 0a3c1a0..a9e8feb 100644 --- a/SwiftValidatorTests/SwiftValidatorTests.swift +++ b/SwiftValidatorTests/SwiftValidatorTests.swift @@ -131,6 +131,14 @@ class SwiftValidatorTests: XCTestCase { XCTAssertFalse(EmailRule().validate(INVALID_PASSWORD), "Password is invalid") } + func testPhoneNumber() { + XCTAssertTrue(PhoneNumberRule().validate("1234567890"), "Phone number should valid") + } + + func testPhoneNumberInvalid() { + XCTAssertFalse(PhoneNumberRule().validate("12345678901"), "Phone number should be invalid") + } + // MARK: Max Length func testMaxLength(){ @@ -158,6 +166,18 @@ class SwiftValidatorTests: XCTestCase { XCTAssertTrue(MinLengthRule(length: 5).validate(LEN_5), "Min Len should be set to 5 and >= length") } + func testExactLength(){ + XCTAssertTrue(ExactLengthRule(length: 5).validate(LEN_5), "Exact Len should be exactly 5") + } + + func testExactLengthInvalidGreaterThan(){ + XCTAssertFalse(ExactLengthRule(length: 6).validate(LEN_5), "Exact Len should be Invalid") + } + + func testExactLengthInvalidLessThan(){ + XCTAssertFalse(ExactLengthRule(length: 4).validate(LEN_5), "Exact Len should be Invalid") + } + // MARK: Full Name func testFullName(){ diff --git a/Validator.xcodeproj/project.pbxproj b/Validator.xcodeproj/project.pbxproj index c9a9cdc..3aaff92 100644 --- a/Validator.xcodeproj/project.pbxproj +++ b/Validator.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 62C1821D1C6312F5003788E7 /* ExactLengthRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C1821C1C6312F5003788E7 /* ExactLengthRule.swift */; settings = {ASSET_TAGS = (); }; }; 62D1AE1D1A1E6D4400E4DFF8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D1AE1C1A1E6D4400E4DFF8 /* AppDelegate.swift */; }; 62D1AE221A1E6D4400E4DFF8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE201A1E6D4400E4DFF8 /* Main.storyboard */; }; 62D1AE241A1E6D4400E4DFF8 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE231A1E6D4400E4DFF8 /* Images.xcassets */; }; @@ -80,6 +81,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 62C1821C1C6312F5003788E7 /* ExactLengthRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExactLengthRule.swift; sourceTree = ""; }; 62D1AE171A1E6D4400E4DFF8 /* Validator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Validator.app; sourceTree = BUILT_PRODUCTS_DIR; }; 62D1AE1B1A1E6D4400E4DFF8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 62D1AE1C1A1E6D4400E4DFF8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -257,6 +259,7 @@ FB465CED1B9889EA00398388 /* Rule.swift */, FB465CEE1B9889EA00398388 /* ValidationRule.swift */, FB465CEF1B9889EA00398388 /* ZipCodeRule.swift */, + 62C1821C1C6312F5003788E7 /* ExactLengthRule.swift */, ); path = Rules; sourceTree = ""; @@ -476,6 +479,7 @@ FB465CFB1B9889EA00398388 /* RegexRule.swift in Sources */, FB465CF81B9889EA00398388 /* MinLengthRule.swift in Sources */, FB465CF71B9889EA00398388 /* MaxLengthRule.swift in Sources */, + 62C1821D1C6312F5003788E7 /* ExactLengthRule.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };