From 20f1238e66bbd2ac2da9212243d162dd2bcb71a6 Mon Sep 17 00:00:00 2001 From: Guilherme Berger Date: Mon, 6 Apr 2015 04:24:39 -0400 Subject: [PATCH 1/2] Create MaxLengthRule.swift --- Validator/MaxLengthRule.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Validator/MaxLengthRule.swift diff --git a/Validator/MaxLengthRule.swift b/Validator/MaxLengthRule.swift new file mode 100644 index 0000000..ba4fa1c --- /dev/null +++ b/Validator/MaxLengthRule.swift @@ -0,0 +1,27 @@ +// +// MaxLengthRule.swift +// Validator +// +// Created by Guilherme Berger on 4/6/15. +// + +import Foundation + +class MaxLengthRule: Rule { + + private var DEFAULT_MAX_LENGTH: Int = 16 + + init(){} + + init(length: Int){ + self.DEFAULT_MAX_LENGTH = length + } + + func validate(value: String) -> Bool { + return countElements(value) <= DEFAULT_MAX_LENGTH + } + + func errorMessage() -> String { + return "Must be at most \(DEFAULT_MAX_LENGTH) characters long" + } +} From e09ee70c96fcb5c654e077594b49c3c10f3d08ef Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Fri, 10 Apr 2015 10:55:04 -0600 Subject: [PATCH 2/2] Adding max length rule and fixing method access to make it public --- Validator.xcodeproj/project.pbxproj | 4 ++++ Validator/MaxLengthRule.swift | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Validator.xcodeproj/project.pbxproj b/Validator.xcodeproj/project.pbxproj index 61bb899..f0feb2f 100644 --- a/Validator.xcodeproj/project.pbxproj +++ b/Validator.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 623F29941AD836E6005B26CB /* MaxLengthRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623F29931AD836E6005B26CB /* MaxLengthRule.swift */; }; 628637261AAA474B00BC8FCF /* MinLengthRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628637251AAA474B00BC8FCF /* MinLengthRule.swift */; }; 628637281AAA49E300BC8FCF /* ConfirmRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628637271AAA49E300BC8FCF /* ConfirmRule.swift */; }; 62D1AE1D1A1E6D4400E4DFF8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D1AE1C1A1E6D4400E4DFF8 /* AppDelegate.swift */; }; @@ -38,6 +39,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 623F29931AD836E6005B26CB /* MaxLengthRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaxLengthRule.swift; sourceTree = ""; }; 628637251AAA474B00BC8FCF /* MinLengthRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MinLengthRule.swift; sourceTree = ""; }; 628637271AAA49E300BC8FCF /* ConfirmRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfirmRule.swift; sourceTree = ""; }; 62D1AE171A1E6D4400E4DFF8 /* Validator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Validator.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -159,6 +161,7 @@ 62DC8D661AAA42700095DFA7 /* Rules */ = { isa = PBXGroup; children = ( + 623F29931AD836E6005B26CB /* MaxLengthRule.swift */, 628637251AAA474B00BC8FCF /* MinLengthRule.swift */, 62DC8D641AAA42520095DFA7 /* Rule.swift */, 62DC8D691AAA42CE0095DFA7 /* EmailRule.swift */, @@ -286,6 +289,7 @@ 628637261AAA474B00BC8FCF /* MinLengthRule.swift in Sources */, 62DC8D711AAA43110095DFA7 /* ZipCodeRule.swift in Sources */, 62D1AE521A1E6FF800E4DFF8 /* ValidationRule.swift in Sources */, + 623F29941AD836E6005B26CB /* MaxLengthRule.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Validator/MaxLengthRule.swift b/Validator/MaxLengthRule.swift index ba4fa1c..a659f3b 100644 --- a/Validator/MaxLengthRule.swift +++ b/Validator/MaxLengthRule.swift @@ -7,21 +7,21 @@ import Foundation -class MaxLengthRule: Rule { +public class MaxLengthRule: Rule { private var DEFAULT_MAX_LENGTH: Int = 16 - init(){} + public init(){} - init(length: Int){ + public init(length: Int){ self.DEFAULT_MAX_LENGTH = length } - func validate(value: String) -> Bool { + public func validate(value: String) -> Bool { return countElements(value) <= DEFAULT_MAX_LENGTH } - func errorMessage() -> String { + public func errorMessage() -> String { return "Must be at most \(DEFAULT_MAX_LENGTH) characters long" } }