Skip to content

Commit 847885a

Browse files
added float rule with tests
1 parent ab0f1f1 commit 847885a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Validator.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */; };
2727
62DC8D711AAA43110095DFA7 /* ZipCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */; };
2828
62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E9E2AC1ACFB336000A939C /* RegexRule.swift */; };
29+
DC5A35EC1AF99BA60013FE6B /* FloatRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */; };
2930
/* End PBXBuildFile section */
3031

3132
/* Begin PBXContainerItemProxy section */
@@ -62,6 +63,7 @@
6263
62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRule.swift; sourceTree = "<group>"; };
6364
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = "<group>"; };
6465
62E9E2AC1ACFB336000A939C /* RegexRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexRule.swift; sourceTree = "<group>"; };
66+
DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FloatRule.swift; sourceTree = "<group>"; };
6567
/* End PBXFileReference section */
6668

6769
/* Begin PBXFrameworksBuildPhase section */
@@ -171,6 +173,7 @@
171173
62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */,
172174
628637271AAA49E300BC8FCF /* ConfirmRule.swift */,
173175
62E9E2AC1ACFB336000A939C /* RegexRule.swift */,
176+
DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */,
174177
);
175178
name = Rules;
176179
sourceTree = "<group>";
@@ -283,6 +286,7 @@
283286
628637281AAA49E300BC8FCF /* ConfirmRule.swift in Sources */,
284287
62DC8D651AAA42520095DFA7 /* Rule.swift in Sources */,
285288
62D1AE1F1A1E6D4400E4DFF8 /* ViewController.swift in Sources */,
289+
DC5A35EC1AF99BA60013FE6B /* FloatRule.swift in Sources */,
286290
62DC8D6D1AAA42CE0095DFA7 /* RequiredRule.swift in Sources */,
287291
62D1AE1D1A1E6D4400E4DFF8 /* AppDelegate.swift in Sources */,
288292
62D1AE581A1E700200E4DFF8 /* Validator.swift in Sources */,

Validator/FloatRule.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// FloatRule.swift
3+
// Validator
4+
//
5+
// Created by Cameron McCord on 5/5/15.
6+
// Copyright (c) 2015 jpotts18. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public class FloatRule:Rule {
12+
13+
public init(){
14+
15+
}
16+
17+
public func validate(value: String) -> Bool {
18+
let regex = NSRegularExpression(pattern: "[-+]?(\\d*[.])?\\d+", options: nil, error: nil)
19+
if let regex = regex {
20+
let match = regex.numberOfMatchesInString(value, options: nil, range: NSRange(location: 0, length: count(value)))
21+
return match == 1
22+
}
23+
return false
24+
}
25+
26+
public func errorMessage() -> String {
27+
return "This must be a number with or without a decimal"
28+
}
29+
}

ValidatorTests/ValidatorTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class ValidatorTests: XCTestCase {
2727
let VALID_PASSWORD = "Super$ecret"
2828
let INVALID_PASSWORD = "abc"
2929

30+
let VALID_FLOAT = "1234.444"
31+
let INVALID_FLOAT = "1234.44.44"
32+
3033
let LEN_3 = "hey"
3134
let LEN_5 = "Howdy"
3235
let LEN_20 = "Paint the cat orange"
@@ -94,6 +97,17 @@ class ValidatorTests: XCTestCase {
9497
XCTAssertFalse(EmailRule().validate(INVALID_EMAIL), "Email should be invalid")
9598
}
9699

100+
// MARK: Float
101+
102+
func testFloat() {
103+
XCTAssert(FloatRule().validate(VALID_FLOAT), "Float should be valid")
104+
}
105+
106+
func testFloatInvalid() {
107+
XCTAssert(!FloatRule().validate(INVALID_FLOAT), "Float should be invalid")
108+
XCTAssert(!FloatRule().validate(VALID_EMAIL), "Float should be invalid")
109+
}
110+
97111
// MARK: Confirm against field
98112

99113
func testConfirmSame(){

0 commit comments

Comments
 (0)