Skip to content

Commit

Permalink
version 2.2.0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gkaimakas committed May 19, 2016
1 parent a7b5e08 commit 79f77b1
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 55 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,11 @@
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).

## [2.2.0] - May 19th 2016
- Added CHANGELOG
- NEW Validator: `regex(pattern: String)`. The `value` is checked from start to finish with the `pattern`.
- Minor updates and typos


## [2.1.0] - May 10th 2016
- Added logical operators to combine Validators. The available logical operators are: `&&`, `||`, `!` (AND, OR, NOT respectively)
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ String validation for iOS developed in Swift. Inspired by [validator.js](https:/
+ [Walkthrough](#walkthrough)
+ [Usage](#usage)
+ [Configuration](#configuration)
+ [Supported function](#supported-functions)
+ [Supported functions](#supported-functions)
+ [License MIT](#license-mit)

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

*FQDNOptions is a class that is used on isFQDN for configuration purposes. It can be instantiated like this:
Expand Down
2 changes: 1 addition & 1 deletion SwiftValidators.podspec
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "SwiftValidators"
s.version = "2.1.0"
s.version = "2.2.0"
s.summary = "String validation for iOS developed in Swift. Inspired by validator.js"
s.homepage = "https://github.com/gkaimakas/SwiftValidators"
s.license = 'MIT'
Expand Down
4 changes: 4 additions & 0 deletions SwiftValidators.xcodeproj/project.pbxproj
Expand Up @@ -13,6 +13,7 @@
131584E21B5FFD7F0077E959 /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 131584E11B5FFD7F0077E959 /* Validator.swift */; };
13E80BC81B6BB64D001DFEA4 /* SwiftValidators.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 13E80BC71B6BB64D001DFEA4 /* SwiftValidators.podspec */; };
13E80BCA1B6BB694001DFEA4 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 13E80BC91B6BB694001DFEA4 /* LICENSE */; };
13F2903D1CEDA9A50081F851 /* CHANGELOG.md in Resources */ = {isa = PBXBuildFile; fileRef = 13F2903B1CEDA1080081F851 /* CHANGELOG.md */; };
D73D6CBEA6F4C4B64AE3C787 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = D73D62E7F646ACCD88ABA593 /* README.md */; };
D73D6E5F64547D2C5557336A /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73D6757670941DC51ADBD44 /* Extensions.swift */; };
FFA01A3BC9BFC00F54CFCD27 /* Pods_SwiftValidatorsTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D590C794FFC3FF9BF8D6D1C9 /* Pods_SwiftValidatorsTests.framework */; };
Expand All @@ -39,6 +40,7 @@
131584E11B5FFD7F0077E959 /* Validator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = "<group>"; };
13E80BC71B6BB64D001DFEA4 /* SwiftValidators.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SwiftValidators.podspec; sourceTree = "<group>"; };
13E80BC91B6BB694001DFEA4 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
13F2903B1CEDA1080081F851 /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
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>"; };
D590C794FFC3FF9BF8D6D1C9 /* Pods_SwiftValidatorsTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftValidatorsTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D73D62E7F646ACCD88ABA593 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = file.md; path = README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -69,6 +71,7 @@
131584BB1B5FFCB60077E959 = {
isa = PBXGroup;
children = (
13F2903B1CEDA1080081F851 /* CHANGELOG.md */,
13E80BC71B6BB64D001DFEA4 /* SwiftValidators.podspec */,
D73D62E7F646ACCD88ABA593 /* README.md */,
13E80BC91B6BB694001DFEA4 /* LICENSE */,
Expand Down Expand Up @@ -237,6 +240,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
13F2903D1CEDA9A50081F851 /* CHANGELOG.md in Resources */,
13E80BCA1B6BB694001DFEA4 /* LICENSE in Resources */,
D73D6CBEA6F4C4B64AE3C787 /* README.md in Resources */,
13E80BC81B6BB64D001DFEA4 /* SwiftValidators.podspec in Resources */,
Expand Down
Binary file not shown.
90 changes: 37 additions & 53 deletions SwiftValidators/Validator.swift
Expand Up @@ -60,9 +60,9 @@ public class Validator {
private static let defaultValidator: Validator = Validator()

private static let
ΕmailRegex: String = "[\\w._%+-|]+@[\\w0-9.-]+\\.[A-Za-z]{2,6}",
ΑlphaRegex: String = "[a-zA-Z]+",
Βase64Regex: String = "(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?",
EmailRegex: String = "[\\w._%+-|]+@[\\w0-9.-]+\\.[A-Za-z]{2,6}",
AlphaRegex: String = "[a-zA-Z]+",
Base64Regex: String = "(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?",
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})",
HexColorRegex: String = "#?([0-9A-F]{3}|[0-9A-F]{6})",
HexadecimalRegex: String = "[0-9A-F]+",
Expand Down Expand Up @@ -431,6 +431,16 @@ public class Validator {
public static var required: Validation {
return Validator.defaultValidator.required
}

/**
check if the value fullfils the pattern. The value is matched from start to finish with the regex.
- parameter pattern: The regex to check
- returns: (String) -> Bool
*/
public static func regex(pattern: String) -> Validation {
return Validator.defaultValidator.regex(pattern)
}


// ------------------------ //
Expand Down Expand Up @@ -556,15 +566,7 @@ public class Validator {
- returns: (String)->Bool
*/
public var isAlpha: Validation {
return {
(value: String) in
if value == "" {
return (self.validationMode == .Default ? true : false)
}

let test = NSPredicate(format: "SELF MATCHES %@", Validator.ΑlphaRegex)
return test.evaluateWithObject(value)
}
return regex(Validator.AlphaRegex)
}

/**
Expand All @@ -573,13 +575,7 @@ public class Validator {
- returns: (String)->Bool
*/
public var isAlphanumeric: Validation {
return {
(value: String) in
if value == "" {
return (self.validationMode == .Default ? true : false)
}
return self.regexTest(Validator.AlphanumericRegex, value)
}
return regex(Validator.AlphanumericRegex)
}

/**
Expand All @@ -588,15 +584,7 @@ public class Validator {
- returns: (String)->Bool
*/
public var isBase64: Validation {
return {
(value: String) in
if value == "" {
return (self.validationMode == .Default ? true : false)
}

let test = NSPredicate(format: "SELF MATCHES %@", Validator.Βase64Regex)
return test.evaluateWithObject(value)
}
return self.regex(Validator.Base64Regex)
}

/**
Expand Down Expand Up @@ -695,16 +683,7 @@ public class Validator {
- returns: (String)->Bool
*/
public var isEmail: Validation {
return {
(value: String) -> Bool in

if value == "" {
return (self.validationMode == .Default ? true : false)
}

let emailTest = NSPredicate(format: "SELF MATCHES %@", Validator.ΕmailRegex)
return emailTest.evaluateWithObject(value)
}
return self.regex(Validator.EmailRegex)
}


Expand Down Expand Up @@ -1065,13 +1044,7 @@ public class Validator {
- returns: (String)->Bool
*/
public var isNumeric: Validation {
return {
(value: String) in
if value == "" {
return (self.validationMode == .Default ? true : false)
}
return self.regexTest(Validator.NumericRegex, value)
}
return regex(Validator.NumericRegex)
}

/**
Expand All @@ -1081,13 +1054,7 @@ public class Validator {
- returns: (String)->Bool
*/
public func isPhone(locale: String) -> Validation {
return {
(value: String) in
if value == "" {
return (self.validationMode == .Default ? true : false)
}
return self.regexTest(Validator.PhoneRegex[locale]!, value)
}
return regex(Validator.PhoneRegex[locale]!)
}


Expand Down Expand Up @@ -1178,8 +1145,25 @@ public class Validator {
return value.characters.count >= length ? true : false
}
}

/**
check if the value fullfils the pattern. The value is matched from start to finish with the regex.
- parameter pattern: The regex to check
- returns: (String) -> Bool
*/
public func regex(pattern: String) -> Validation {
return {(value: String) -> Bool in
if value == "" {
return (self.validationMode == .Default ? true : false)
}

let regexText = NSPredicate(format: "SELF MATCHES %@", pattern)
return regexText.evaluateWithObject(value)
}
}

/**
checks if it is not an empty string
Expand Down
10 changes: 10 additions & 0 deletions SwiftValidatorsTests/SwiftValidatorsTests.swift
Expand Up @@ -194,6 +194,16 @@ class ValidatorSpec : QuickSpec {
expect(Validator.required("")).to(equal(false))
}
}

describe("regex") {
it("should check if the value matches the pattern") {
expect(Validator.regex("ab|bc|ca")("ab")) == true
expect(Validator.regex("ab|bc|ca")("bc")) == true
expect(Validator.regex("ab|bc|ca")("ca")) == true

expect(Validator.regex("ab|bc|ca")("ba")) == false
}
}

describe("isUUID"){
it("should check if it is a valid UUID"){
Expand Down

0 comments on commit 79f77b1

Please sign in to comment.