Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 79f77b1

Browse files
committed
version 2.2.0
added a changelog, added a new Validator `regex(pattern: String)`, added unit tests for `regex(pattern: String)` fixed a typos in `EmailRegex`, `Base64Regex` where there were mixed english with greek characters, updated `isEmail`, `isBase64`, `isAlpha`, `isNumeric`, `isPhone` to use the new `regex` validator
1 parent a7b5e08 commit 79f77b1

File tree

7 files changed

+65
-55
lines changed

7 files changed

+65
-55
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log
2+
This project adheres to [Semantic Versioning](http://semver.org/).
3+
4+
## [2.2.0] - May 19th 2016
5+
- Added CHANGELOG
6+
- NEW Validator: `regex(pattern: String)`. The `value` is checked from start to finish with the `pattern`.
7+
- Minor updates and typos
8+
9+
10+
## [2.1.0] - May 10th 2016
11+
- Added logical operators to combine Validators. The available logical operators are: `&&`, `||`, `!` (AND, OR, NOT respectively)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ String validation for iOS developed in Swift. Inspired by [validator.js](https:/
99
+ [Walkthrough](#walkthrough)
1010
+ [Usage](#usage)
1111
+ [Configuration](#configuration)
12-
+ [Supported function](#supported-functions)
12+
+ [Supported functions](#supported-functions)
1313
+ [License MIT](#license-mit)
1414

1515
### Installation
@@ -142,6 +142,7 @@ isUppercase|checks if has only uppercase letter|var|-|Validator.isUppercase("ABC
142142
maxLength|checks if the length does not exceed the max length|func|Int|Validator.maxLength(2)("ab")
143143
minLength|checks if the length isn't lower than|func|Int|Validator.minLength(1)("213")
144144
required|checks if it is not an empty string|var|-|Validator.required("")
145+
regex| checks that the value matches the regex from start to finish| func | String | Validator.regex(pattern)
145146
watch| check the delegate for equality | func | ValidatorProtocol | Validator.watch(delegate)
146147

147148
*FQDNOptions is a class that is used on isFQDN for configuration purposes. It can be instantiated like this:

SwiftValidators.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = "SwiftValidators"
4-
s.version = "2.1.0"
4+
s.version = "2.2.0"
55
s.summary = "String validation for iOS developed in Swift. Inspired by validator.js"
66
s.homepage = "https://github.com/gkaimakas/SwiftValidators"
77
s.license = 'MIT'

SwiftValidators.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
131584E21B5FFD7F0077E959 /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 131584E11B5FFD7F0077E959 /* Validator.swift */; };
1414
13E80BC81B6BB64D001DFEA4 /* SwiftValidators.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 13E80BC71B6BB64D001DFEA4 /* SwiftValidators.podspec */; };
1515
13E80BCA1B6BB694001DFEA4 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 13E80BC91B6BB694001DFEA4 /* LICENSE */; };
16+
13F2903D1CEDA9A50081F851 /* CHANGELOG.md in Resources */ = {isa = PBXBuildFile; fileRef = 13F2903B1CEDA1080081F851 /* CHANGELOG.md */; };
1617
D73D6CBEA6F4C4B64AE3C787 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = D73D62E7F646ACCD88ABA593 /* README.md */; };
1718
D73D6E5F64547D2C5557336A /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73D6757670941DC51ADBD44 /* Extensions.swift */; };
1819
FFA01A3BC9BFC00F54CFCD27 /* Pods_SwiftValidatorsTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D590C794FFC3FF9BF8D6D1C9 /* Pods_SwiftValidatorsTests.framework */; };
@@ -39,6 +40,7 @@
3940
131584E11B5FFD7F0077E959 /* Validator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = "<group>"; };
4041
13E80BC71B6BB64D001DFEA4 /* SwiftValidators.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SwiftValidators.podspec; sourceTree = "<group>"; };
4142
13E80BC91B6BB694001DFEA4 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
43+
13F2903B1CEDA1080081F851 /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
4244
5F12C8F44A0B081C221B029B /* Pods-SwiftValidatorsTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftValidatorsTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftValidatorsTests/Pods-SwiftValidatorsTests.release.xcconfig"; sourceTree = "<group>"; };
4345
D590C794FFC3FF9BF8D6D1C9 /* Pods_SwiftValidatorsTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftValidatorsTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4446
D73D62E7F646ACCD88ABA593 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = file.md; path = README.md; sourceTree = "<group>"; };
@@ -69,6 +71,7 @@
6971
131584BB1B5FFCB60077E959 = {
7072
isa = PBXGroup;
7173
children = (
74+
13F2903B1CEDA1080081F851 /* CHANGELOG.md */,
7275
13E80BC71B6BB64D001DFEA4 /* SwiftValidators.podspec */,
7376
D73D62E7F646ACCD88ABA593 /* README.md */,
7477
13E80BC91B6BB694001DFEA4 /* LICENSE */,
@@ -237,6 +240,7 @@
237240
isa = PBXResourcesBuildPhase;
238241
buildActionMask = 2147483647;
239242
files = (
243+
13F2903D1CEDA9A50081F851 /* CHANGELOG.md in Resources */,
240244
13E80BCA1B6BB694001DFEA4 /* LICENSE in Resources */,
241245
D73D6CBEA6F4C4B64AE3C787 /* README.md in Resources */,
242246
13E80BC81B6BB64D001DFEA4 /* SwiftValidators.podspec in Resources */,
Binary file not shown.

SwiftValidators/Validator.swift

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public class Validator {
6060
private static let defaultValidator: Validator = Validator()
6161

6262
private static let
63-
ΕmailRegex: String = "[\\w._%+-|]+@[\\w0-9.-]+\\.[A-Za-z]{2,6}",
64-
ΑlphaRegex: String = "[a-zA-Z]+",
65-
Βase64Regex: String = "(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?",
63+
EmailRegex: String = "[\\w._%+-|]+@[\\w0-9.-]+\\.[A-Za-z]{2,6}",
64+
AlphaRegex: String = "[a-zA-Z]+",
65+
Base64Regex: String = "(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?",
6666
CreditCardRegex: String = "(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})",
6767
HexColorRegex: String = "#?([0-9A-F]{3}|[0-9A-F]{6})",
6868
HexadecimalRegex: String = "[0-9A-F]+",
@@ -431,6 +431,16 @@ public class Validator {
431431
public static var required: Validation {
432432
return Validator.defaultValidator.required
433433
}
434+
435+
/**
436+
check if the value fullfils the pattern. The value is matched from start to finish with the regex.
437+
438+
- parameter pattern: The regex to check
439+
- returns: (String) -> Bool
440+
*/
441+
public static func regex(pattern: String) -> Validation {
442+
return Validator.defaultValidator.regex(pattern)
443+
}
434444

435445

436446
// ------------------------ //
@@ -556,15 +566,7 @@ public class Validator {
556566
- returns: (String)->Bool
557567
*/
558568
public var isAlpha: Validation {
559-
return {
560-
(value: String) in
561-
if value == "" {
562-
return (self.validationMode == .Default ? true : false)
563-
}
564-
565-
let test = NSPredicate(format: "SELF MATCHES %@", Validator.ΑlphaRegex)
566-
return test.evaluateWithObject(value)
567-
}
569+
return regex(Validator.AlphaRegex)
568570
}
569571

570572
/**
@@ -573,13 +575,7 @@ public class Validator {
573575
- returns: (String)->Bool
574576
*/
575577
public var isAlphanumeric: Validation {
576-
return {
577-
(value: String) in
578-
if value == "" {
579-
return (self.validationMode == .Default ? true : false)
580-
}
581-
return self.regexTest(Validator.AlphanumericRegex, value)
582-
}
578+
return regex(Validator.AlphanumericRegex)
583579
}
584580

585581
/**
@@ -588,15 +584,7 @@ public class Validator {
588584
- returns: (String)->Bool
589585
*/
590586
public var isBase64: Validation {
591-
return {
592-
(value: String) in
593-
if value == "" {
594-
return (self.validationMode == .Default ? true : false)
595-
}
596-
597-
let test = NSPredicate(format: "SELF MATCHES %@", Validator.Βase64Regex)
598-
return test.evaluateWithObject(value)
599-
}
587+
return self.regex(Validator.Base64Regex)
600588
}
601589

602590
/**
@@ -695,16 +683,7 @@ public class Validator {
695683
- returns: (String)->Bool
696684
*/
697685
public var isEmail: Validation {
698-
return {
699-
(value: String) -> Bool in
700-
701-
if value == "" {
702-
return (self.validationMode == .Default ? true : false)
703-
}
704-
705-
let emailTest = NSPredicate(format: "SELF MATCHES %@", Validator.ΕmailRegex)
706-
return emailTest.evaluateWithObject(value)
707-
}
686+
return self.regex(Validator.EmailRegex)
708687
}
709688

710689

@@ -1065,13 +1044,7 @@ public class Validator {
10651044
- returns: (String)->Bool
10661045
*/
10671046
public var isNumeric: Validation {
1068-
return {
1069-
(value: String) in
1070-
if value == "" {
1071-
return (self.validationMode == .Default ? true : false)
1072-
}
1073-
return self.regexTest(Validator.NumericRegex, value)
1074-
}
1047+
return regex(Validator.NumericRegex)
10751048
}
10761049

10771050
/**
@@ -1081,13 +1054,7 @@ public class Validator {
10811054
- returns: (String)->Bool
10821055
*/
10831056
public func isPhone(locale: String) -> Validation {
1084-
return {
1085-
(value: String) in
1086-
if value == "" {
1087-
return (self.validationMode == .Default ? true : false)
1088-
}
1089-
return self.regexTest(Validator.PhoneRegex[locale]!, value)
1090-
}
1057+
return regex(Validator.PhoneRegex[locale]!)
10911058
}
10921059

10931060

@@ -1178,8 +1145,25 @@ public class Validator {
11781145
return value.characters.count >= length ? true : false
11791146
}
11801147
}
1148+
1149+
/**
1150+
check if the value fullfils the pattern. The value is matched from start to finish with the regex.
1151+
1152+
- parameter pattern: The regex to check
1153+
- returns: (String) -> Bool
1154+
*/
1155+
public func regex(pattern: String) -> Validation {
1156+
return {(value: String) -> Bool in
1157+
if value == "" {
1158+
return (self.validationMode == .Default ? true : false)
1159+
}
1160+
1161+
let regexText = NSPredicate(format: "SELF MATCHES %@", pattern)
1162+
return regexText.evaluateWithObject(value)
1163+
}
1164+
}
11811165

1182-
1166+
11831167
/**
11841168
checks if it is not an empty string
11851169

SwiftValidatorsTests/SwiftValidatorsTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ class ValidatorSpec : QuickSpec {
194194
expect(Validator.required("")).to(equal(false))
195195
}
196196
}
197+
198+
describe("regex") {
199+
it("should check if the value matches the pattern") {
200+
expect(Validator.regex("ab|bc|ca")("ab")) == true
201+
expect(Validator.regex("ab|bc|ca")("bc")) == true
202+
expect(Validator.regex("ab|bc|ca")("ca")) == true
203+
204+
expect(Validator.regex("ab|bc|ca")("ba")) == false
205+
}
206+
}
197207

198208
describe("isUUID"){
199209
it("should check if it is a valid UUID"){

0 commit comments

Comments
 (0)