Merge branch 'bqmackay-Copyright'

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
This commit is contained in:
Jeff Potter 2015-04-04 00:10:58 -06:00
commit f51744a90f
14 changed files with 83 additions and 111 deletions

View File

@ -24,6 +24,7 @@
62DC8D6D1AAA42CE0095DFA7 /* RequiredRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6A1AAA42CE0095DFA7 /* RequiredRule.swift */; };
62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */; };
62DC8D711AAA43110095DFA7 /* ZipCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */; };
62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E9E2AC1ACFB336000A939C /* RegexRule.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -58,6 +59,7 @@
62DC8D6A1AAA42CE0095DFA7 /* RequiredRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequiredRule.swift; sourceTree = "<group>"; };
62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRule.swift; sourceTree = "<group>"; };
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = "<group>"; };
62E9E2AC1ACFB336000A939C /* RegexRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexRule.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -165,6 +167,7 @@
62DC8D671AAA42920095DFA7 /* FullNameRule.swift */,
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */,
628637271AAA49E300BC8FCF /* ConfirmRule.swift */,
62E9E2AC1ACFB336000A939C /* RegexRule.swift */,
);
name = Rules;
sourceTree = "<group>";
@ -270,6 +273,7 @@
buildActionMask = 2147483647;
files = (
62DC8D681AAA42920095DFA7 /* FullNameRule.swift in Sources */,
62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */,
62D1AE4F1A1E6FF800E4DFF8 /* ValidationError.swift in Sources */,
62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */,
62DC8D6C1AAA42CE0095DFA7 /* EmailRule.swift in Sources */,

View File

@ -18,15 +18,10 @@ public class ConfirmationRule: Rule {
}
public func validate(value: String) -> Bool {
if self.confirmField.text == value {
return true
} else {
return false
}
return confirmField.text == value
}
public func errorMessage() -> String {
return "This field does not match"
}
}

View File

@ -3,32 +3,22 @@
// Pingo
//
// Created by Jeff Potter on 11/11/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
public class EmailRule: Rule {
private let REGEX: String
public class EmailRule: RegexRule {
public init(){
self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
super.init(regex: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}")
}
public init(regex:String){
REGEX = regex
override public init(regex:String){
super.init(regex: regex)
}
public func validate(value: String) -> Bool {
if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) {
if test.evaluateWithObject(value) {
return true
}
}
return false
}
public func errorMessage() -> String {
override public func errorMessage() -> String {
return "Must be a valid email address"
}
}

View File

@ -3,7 +3,7 @@
// Pingo
//
// Created by Jeff Potter on 11/19/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
@ -15,13 +15,9 @@ public class FullNameRule : Rule {
return "Please provide a first & last name"
}
public func validate(value:String) -> Bool {
var nameArray:[String] = split(value) { $0 == " " }
if nameArray.count >= 2 {
return true
}
return false
public func validate(value: String) -> Bool {
var nameArray: [String] = split(value) { $0 == " " }
return nameArray.count >= 2
}
public func errorMessage() -> String {
return message

View File

@ -8,24 +8,18 @@
import Foundation
class MinLengthRule : Rule {
class MinLengthRule: Rule {
private let DEFAULT_MIN_LENGTH: Int
private var DEFAULT_MIN_LENGTH: Int = 3
init(){
DEFAULT_MIN_LENGTH = 3
}
init(){}
init(length:Int){
init(length: Int){
self.DEFAULT_MIN_LENGTH = length
}
func validate(value: String) -> Bool {
if countElements(value) < DEFAULT_MIN_LENGTH {
return false
}
return true
return countElements(value) > DEFAULT_MIN_LENGTH
}
func errorMessage() -> String {

View File

@ -3,43 +3,31 @@
// Pingo
//
// Created by Jeff Potter on 11/13/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
public class PasswordRule : Rule {
public class PasswordRule : RegexRule {
// Alternative Regexes
// 8 characters. One uppercase. One Lowercase. One number.
// var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).{8,}$"
//
// no length. One uppercase. One lowercae. One number.
// var PASSWORD_REGEX = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$"
// 8 characters. one uppercase
private let REGEX: String
public init(){
self.REGEX = "^(?=.*?[A-Z]).{8,}$"
super.init(regex: "^(?=.*?[A-Z]).{8,}$")
}
public init(regex:String){
self.REGEX = regex
}
public func validate(value: String) -> Bool {
if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) {
if test.evaluateWithObject(value) {
return true
}
}
return false
override public init(regex: String) {
super.init(regex: regex)
}
public func errorMessage() -> String {
override public func errorMessage() -> String {
return "Must be 8 characters with 1 uppercase"
}
}

31
Validator/RegexRule.swift Normal file
View File

@ -0,0 +1,31 @@
//
// RegexRule.swift
// Validator
//
// Created by Jeff Potter on 4/3/15.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
public class RegexRule : Rule {
private var REGEX: String = "^(?=.*?[A-Z]).{8,}$"
init(regex: String){
self.REGEX = regex
}
public func validate(value: String) -> Bool {
if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) {
if test.evaluateWithObject(value) {
return true
}
}
return false
}
public func errorMessage() -> String {
return "Invalid Regular Expression"
}
}

View File

@ -3,12 +3,11 @@
// pyur-ios
//
// Created by Jeff Potter on 12/22/14.
// Copyright (c) 2014 ringseven. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
public class RequiredRule: Rule {
public init(){}
@ -17,11 +16,8 @@ public class RequiredRule: Rule {
return "This field is required"
}
public func validate(value:String) -> Bool {
if value.isEmpty {
return false
}
return true
public func validate(value: String) -> Bool {
return !value.isEmpty
}
public func errorMessage() -> String {

View File

@ -3,12 +3,12 @@
// Pingo
//
// Created by Jeff Potter on 11/11/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
public protocol Rule {
func validate(value:String) -> Bool
func validate(value: String) -> Bool
func errorMessage() -> String
}

View File

@ -3,7 +3,7 @@
// Pingo
//
// Created by Jeff Potter on 11/11/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
@ -18,5 +18,4 @@ public class ValidationError {
self.textField = textField
self.errorMessage = error
}
}

View File

@ -3,7 +3,7 @@
// Pingo
//
// Created by Jeff Potter on 11/11/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
@ -22,12 +22,10 @@ public class ValidationRule {
public func validateField() -> ValidationError? {
for rule in rules {
var isValid:Bool = rule.validate(textField.text)
if !isValid {
if !rule.validate(textField.text) {
return ValidationError(textField: self.textField, error: rule.errorMessage())
}
}
return nil
}
}

View File

@ -3,7 +3,7 @@
// Pingo
//
// Created by Jeff Potter on 11/10/14.
// Copyright (c) 2014 Byron Mackay. All rights reserved.
// Copyright (c) 2015 jpotts18. All rights reserved.
//
import Foundation
@ -11,7 +11,7 @@ import UIKit
@objc public protocol ValidationDelegate {
func validationWasSuccessful()
func validationFailed(errors:[UITextField:ValidationError])
func validationFailed(errors: [UITextField:ValidationError])
}
public class Validator {
@ -34,9 +34,9 @@ public class Validator {
public func validateAll(delegate:ValidationDelegate) {
for field in validations.keys {
if let currentRule:ValidationRule = validations[field] {
if var error:ValidationError = currentRule.validateField() {
if (currentRule.errorLabel != nil) {
if let currentRule: ValidationRule = validations[field] {
if var error: ValidationError = currentRule.validateField() {
if currentRule.errorLabel != nil {
error.errorLabel = currentRule.errorLabel
}
errors[field] = error
@ -53,8 +53,7 @@ public class Validator {
}
}
func clearErrors(){
func clearErrors() {
self.errors = [:]
}
}

View File

@ -41,12 +41,6 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func submitTapped(sender: AnyObject) {
println("Validating...")
self.clearErrors()
@ -101,6 +95,4 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
func hideKeyboard(){
self.view.endEditing(true)
}
}
}

View File

@ -8,27 +8,17 @@
import Foundation
public class ZipCodeRule: Rule {
private let REGEX: String
public class ZipCodeRule: RegexRule {
init(){
self.REGEX = "\\d{5}"
}
init(regex:String){
self.REGEX = regex
}
public func validate(value: String) -> Bool {
if let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX) {
if test.evaluateWithObject(value) {
return true
}
}
return false
public init(){
super.init(regex: "\\d{5}")
}
public func errorMessage() -> String {
override public init(regex: String) {
super.init(regex: regex)
}
public override func errorMessage() -> String {
return "Enter a valid 5 digit zipcode"
}