ExactLengthRule, PhoneNumber public, Tests added
This commit is contained in:
parent
3621ec85ff
commit
ef44bf5477
|
|
@ -1,7 +1,4 @@
|
|||
//
|
||||
// File.swift
|
||||
// Pingo
|
||||
//
|
||||
// Created by Jeff Potter on 11/11/14.
|
||||
// Copyright (c) 2015 jpotts18. All rights reserved.
|
||||
//
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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}"
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
//
|
||||
// FullNameValidation.swift
|
||||
// Pingo
|
||||
//
|
||||
// Created by Jeff Potter on 11/19/14.
|
||||
// Copyright (c) 2015 jpotts18. All rights reserved.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
//
|
||||
// Validation.swift
|
||||
// Pingo
|
||||
//
|
||||
// Created by Jeff Potter on 11/11/14.
|
||||
// Copyright (c) 2015 jpotts18. All rights reserved.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
//
|
||||
// ValidationRule.swift
|
||||
// Pingo
|
||||
//
|
||||
// Created by Jeff Potter on 11/11/14.
|
||||
// Copyright (c) 2015 jpotts18. All rights reserved.
|
||||
|
|
|
|||
|
|
@ -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(){
|
||||
|
|
|
|||
|
|
@ -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 = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
62D1AE1C1A1E6D4400E4DFF8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||
|
|
@ -257,6 +259,7 @@
|
|||
FB465CED1B9889EA00398388 /* Rule.swift */,
|
||||
FB465CEE1B9889EA00398388 /* ValidationRule.swift */,
|
||||
FB465CEF1B9889EA00398388 /* ZipCodeRule.swift */,
|
||||
62C1821C1C6312F5003788E7 /* ExactLengthRule.swift */,
|
||||
);
|
||||
path = Rules;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue