Skip to content

Commit

Permalink
Merge pull request #5 from dtop/feature/rc6
Browse files Browse the repository at this point in the history
Feature/rc6
  • Loading branch information
Danilo Topalovic committed Feb 6, 2016
2 parents fda1954 + 61d6458 commit 836e1d7
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 73 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ By [Danilo Topalovic](http://blog.danilo-topalovic.de).
+ [Required]
+ [Empty]
+ [StrLen]
+ [Charset]
+ [Alnum]
+ [Email]
+ [Regex]
Expand All @@ -39,6 +40,7 @@ Unfortunately I did not find a fancy name for this software so I've called it wh
Heavily inspired by [Zend\Validate] and [Eureka] I started working on this component and I still add tests / validators.
if you encounter any issues or bugs please feel free to report the bug or even write a test for this bug in order to be good again when the bug is solved.

See also the [ExampleProject]

## Requirements

Expand Down Expand Up @@ -131,7 +133,7 @@ see the extraction of the ValidationIteratorTests:
$0.maxLength = 5
}
<~~ ValidatorNumeric() {
$0.canBeString = true
$0.allowString = true
$0.allowFloatingPoint = false
},
forKey: "zipcode"
Expand Down Expand Up @@ -214,20 +216,30 @@ Tests if a given value is between min and max in strlen
+ `errorMessageTooLarge: String`- error message if string is too long

---
#### ValidatorAlnum()
#### ValidatorCharset()

Validates that the given value contains only alphanumerical chars
Validates that the given value contains only chars from the given character set

**configuration**

| value | type | default | description |
|--------------|:----:|---------|--------------------|
| `allowNil` | Bool | true | value an be nil |
| `allowEmpty` | Bool | false | value can be empty |
| value | type | default | description |
|--------------|:--------------:|---------|--------------------|
| `allowNil` | Bool | true | value an be nil |
| `allowEmpty` | Bool | false | value can be empty |
| `charset` | NSCharacterSet | ! | charset to compare |

**Error Messages**

+ `errorMessageStringNotAlnum: String` - (optional)
+ `errorMessageStringDoesNotFit: String` - (optional)

---
### ValidatorAlnum()

Validates if the given value consists of alpha numerical chars only

```
This is a specialization of ValidatorCharset()
```

---
#### ValidatorEmail()
Expand Down Expand Up @@ -499,6 +511,7 @@ class MyGenericValidator<TYPE where TYPE: Equatable>: ValidatorProtocol {
[Zend\Validate]: https://github.com/zendframework/zend-validator
[CocoaPods]: https://cocoapods.org
[WiKi]: https://github.com/dtop/SwiftValidate/wiki
[ExampleProject]: https://github.com/dtop/swift-validate-example

[Introduction]: #introduction
[Requirements]: #requirements
Expand All @@ -508,6 +521,7 @@ class MyGenericValidator<TYPE where TYPE: Equatable>: ValidatorProtocol {
[Required]: #validatorrequired
[Empty]: #validatorempty
[StrLen]: #validatorstrlen
[Charset]: #validatorcharset
[Alnum]: #validatoralnum
[Email]: #validatoremail
[Regex]: #validatorregex
Expand Down
2 changes: 1 addition & 1 deletion SwiftValidate.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftValidate"
s.version = "0.0.5"
s.version = "0.0.6"
s.summary = "A highly customizable validation library for swift"
s.homepage = "http://github.com/dtop/SwiftValidate"
s.license = "MIT"
Expand Down
12 changes: 8 additions & 4 deletions SwiftValidate.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
9F11AE991C2DA30B00D2FB29 /* ValidationIteratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F11AE981C2DA30B00D2FB29 /* ValidationIteratorTests.swift */; };
9F88DF781C2D5C8600E8CA4B /* ValidatorRequired.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F88DF771C2D5C8600E8CA4B /* ValidatorRequired.swift */; };
9F88DF7A1C2D620C00E8CA4B /* ValidatorRequiredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F88DF791C2D620C00E8CA4B /* ValidatorRequiredTests.swift */; };
9FA086F21C2F736B009B72AE /* ValidatorCharset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FA086F11C2F736B009B72AE /* ValidatorCharset.swift */; };
9FCB7D801C26021900887033 /* validate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9FCB7D7F1C26021900887033 /* validate.h */; settings = {ATTRIBUTES = (Public, ); }; };
9FCB7D871C26021900887033 /* SwiftValidate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FCB7D7C1C26021900887033 /* SwiftValidate.framework */; };
9FCB7D8C1C26021900887033 /* ValidatorChainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7D8B1C26021900887033 /* ValidatorChainTests.swift */; };
Expand All @@ -41,7 +42,7 @@
9FCB7DAB1C26088200887033 /* ValidatorNumericTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DAA1C26088200887033 /* ValidatorNumericTests.swift */; };
9FCB7DAD1C260AA000887033 /* ValidatorStrLenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DAC1C260AA000887033 /* ValidatorStrLenTests.swift */; };
9FCB7DB11C260F6500887033 /* ValidatorAlnum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DB01C260F6500887033 /* ValidatorAlnum.swift */; };
9FCB7DB31C26120F00887033 /* ValidatorAlnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DB21C26120F00887033 /* ValidatorAlnumTests.swift */; };
9FCB7DB31C26120F00887033 /* ValidatorCharsetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DB21C26120F00887033 /* ValidatorCharsetTests.swift */; };
9FCB7DB51C26152300887033 /* ValidatorBetween.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DB41C26152300887033 /* ValidatorBetween.swift */; };
9FCB7DB71C26177B00887033 /* ValidatorBetweenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DB61C26177B00887033 /* ValidatorBetweenTests.swift */; };
9FCB7DBA1C26295800887033 /* ValidatorCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCB7DB91C26295800887033 /* ValidatorCallback.swift */; };
Expand Down Expand Up @@ -78,6 +79,7 @@
9F11AE981C2DA30B00D2FB29 /* ValidationIteratorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidationIteratorTests.swift; sourceTree = "<group>"; };
9F88DF771C2D5C8600E8CA4B /* ValidatorRequired.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ValidatorRequired.swift; path = Validators/ValidatorRequired.swift; sourceTree = "<group>"; };
9F88DF791C2D620C00E8CA4B /* ValidatorRequiredTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorRequiredTests.swift; sourceTree = "<group>"; };
9FA086F11C2F736B009B72AE /* ValidatorCharset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ValidatorCharset.swift; path = Validators/ValidatorCharset.swift; sourceTree = "<group>"; };
9FCB7D7C1C26021900887033 /* SwiftValidate.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftValidate.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9FCB7D7F1C26021900887033 /* validate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = validate.h; sourceTree = "<group>"; };
9FCB7D811C26021900887033 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -96,7 +98,7 @@
9FCB7DAA1C26088200887033 /* ValidatorNumericTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorNumericTests.swift; sourceTree = "<group>"; };
9FCB7DAC1C260AA000887033 /* ValidatorStrLenTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorStrLenTests.swift; sourceTree = "<group>"; };
9FCB7DB01C260F6500887033 /* ValidatorAlnum.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ValidatorAlnum.swift; path = Validators/ValidatorAlnum.swift; sourceTree = "<group>"; };
9FCB7DB21C26120F00887033 /* ValidatorAlnumTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorAlnumTests.swift; sourceTree = "<group>"; };
9FCB7DB21C26120F00887033 /* ValidatorCharsetTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorCharsetTests.swift; sourceTree = "<group>"; };
9FCB7DB41C26152300887033 /* ValidatorBetween.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ValidatorBetween.swift; path = Validators/ValidatorBetween.swift; sourceTree = "<group>"; };
9FCB7DB61C26177B00887033 /* ValidatorBetweenTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorBetweenTests.swift; sourceTree = "<group>"; };
9FCB7DB91C26295800887033 /* ValidatorCallback.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ValidatorCallback.swift; path = Validators/ValidatorCallback.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -204,7 +206,7 @@
9F0E5D4A1C26BDE500A9E760 /* ValidatorDateTimeTests.swift */,
9FCB7DBB1C262B8F00887033 /* ValidatorCallbackTests.swift */,
9FCB7DB61C26177B00887033 /* ValidatorBetweenTests.swift */,
9FCB7DB21C26120F00887033 /* ValidatorAlnumTests.swift */,
9FCB7DB21C26120F00887033 /* ValidatorCharsetTests.swift */,
9FCB7DAC1C260AA000887033 /* ValidatorStrLenTests.swift */,
9FCB7DAA1C26088200887033 /* ValidatorNumericTests.swift */,
9FCB7DA81C26061400887033 /* ValidatorEmptyTests.swift */,
Expand Down Expand Up @@ -243,6 +245,7 @@
9FCB7DB01C260F6500887033 /* ValidatorAlnum.swift */,
9F0E5D4C1C26C24E00A9E760 /* ValidatorEmail.swift */,
9F0E5D641C26F0F700A9E760 /* ValidatorRegex.swift */,
9FA086F11C2F736B009B72AE /* ValidatorCharset.swift */,
);
name = String;
sourceTree = "<group>";
Expand Down Expand Up @@ -402,6 +405,7 @@
9F11AE971C2D98FF00D2FB29 /* ValidationIterator.swift in Sources */,
9F0E5D651C26F0F700A9E760 /* ValidatorRegex.swift in Sources */,
9FCB7DA41C26025600887033 /* ValidatorStrLen.swift in Sources */,
9FA086F21C2F736B009B72AE /* ValidatorCharset.swift in Sources */,
9FCB7DA21C26025600887033 /* ValidatorEmpty.swift in Sources */,
9F0E5D691C26F80D00A9E760 /* ValidatorDateBetween.swift in Sources */,
9FCB7DBA1C26295800887033 /* ValidatorCallback.swift in Sources */,
Expand All @@ -420,7 +424,7 @@
files = (
9F0E5D4B1C26BDE500A9E760 /* ValidatorDateTimeTests.swift in Sources */,
9F88DF7A1C2D620C00E8CA4B /* ValidatorRequiredTests.swift in Sources */,
9FCB7DB31C26120F00887033 /* ValidatorAlnumTests.swift in Sources */,
9FCB7DB31C26120F00887033 /* ValidatorCharsetTests.swift in Sources */,
9F0E5D671C26F11100A9E760 /* ValidatorRegexTests.swift in Sources */,
9FCB7DB71C26177B00887033 /* ValidatorBetweenTests.swift in Sources */,
9F0E5D5A1C26E28D00A9E760 /* ValidatorGreaterThanTests.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions validate/ValidatorChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ValidatorChain {
public func validate<T: Any>(value: T?, context: [String: Any?]?) -> Bool {

var result = true
self.errors = [String]()

for validator in self.validators {

Expand Down
62 changes: 5 additions & 57 deletions validate/Validators/ValidatorAlnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,16 @@

import Foundation

public class ValidatorAlnum: BaseValidator, ValidatorProtocol {

/// allow nil values
public var allowNil: Bool = true

/// allow only ascii chars
public var allowEmpty: Bool = false

/// error message not alnum
public var errorMessageStringNotAlnum: String = NSLocalizedString("The String contains illegal characters", comment: "ValidatorAlnum - String not alnum")

/// error message no string
public var errorNoString: String = NSLocalizedString("The entered value was no string", comment: "ValidatorAlnum - String no string")
public class ValidatorAlnum: ValidatorCharset {

/**
Required init
- parameter initializer: the initializer callback
Easy init
- returns: the instance
*/
required public init(@noescape _ initializer: ValidatorAlnum -> () = { _ in }) {

super.init()
initializer(self)
}

/**
Validates if the string is alnum
- parameter value: the string
- parameter context: context
- throws: validation errors
- returns: true if alnum
*/
override public func validate<T: Any>(value: T?, context: [String: Any?]?) throws -> Bool {

// reset errors
self.emptyErrors()

if self.allowNil && nil == value {
return true
}

let charSet = NSCharacterSet.alphanumericCharacterSet()

if let strVal = value as? String {

if self.allowEmpty && strVal.isEmpty {
return true
}

if let _ = strVal.rangeOfCharacterFromSet(charSet.invertedSet) {

return self.returnError(self.errorMessageStringNotAlnum)
}

return true
}
required public init(@noescape _ initializer: ValidatorCharset -> () = { _ in }) {

throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unable to validate chars in string incompatible value"])
super.init(initializer)
self.charset = NSCharacterSet.alphanumericCharacterSet()
}
}
75 changes: 75 additions & 0 deletions validate/Validators/ValidatorCharset.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// ValidatorCharset.swift
// SwiftValidate
//
// Created by Danilo Topalovic on 27.12.15.
// Copyright © 2015 Danilo Topalovic. All rights reserved.
//

import Foundation

public class ValidatorCharset: BaseValidator, ValidatorProtocol {

/// allow nil values
public var allowNil: Bool = true

/// allow only ascii chars
public var allowEmpty: Bool = false

// the charset to validate against
public var charset: NSCharacterSet!

/// error message not alnum
public var errorMessageStringDoesNotFit: String = NSLocalizedString("The String contains illegal characters", comment: "ValidatorCharset - String not alnum")

/**
Easy init
- returns: the instance
*/
public required init(@noescape _ initializer: ValidatorCharset -> () = { _ in }) {

super.init()
initializer(self)
}

/**
Validates the given string against the given charset
- parameter value: the value to match
- parameter context: the context
- throws: validation errors
- returns: true if ok
*/
public override func validate<T: Any>(value: T?, context: [String: Any?]?) throws -> Bool {

// reset errors
self.emptyErrors()

if nil == self.charset {
throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "no chaset given"])
}

if self.allowNil && nil == value {
return true
}

if let strVal = value as? String {

if self.allowEmpty && strVal.isEmpty {
return true
}

if let _ = strVal.rangeOfCharacterFromSet(self.charset.invertedSet) {

return self.returnError(self.errorMessageStringDoesNotFit)
}

return true
}

throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unable to validate chars in string incompatible value"])
}
}
7 changes: 7 additions & 0 deletions validateTests/ValidationIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,12 @@ class ValidationIteratorTests: XCTestCase {
if let _ = validationIterator.getErrorsFor(key: "wrong") {
XCTAssert(false, "may not get anything")
}

let secondResult = validationIterator.validate(formResults)
XCTAssertFalse(secondResult)

// bug in rc6
let secondErrors = validationIterator.getAllErrors()
XCTAssertEqual(errors.count, secondErrors.count)
}
}
1 change: 1 addition & 0 deletions validateTests/ValidatorChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ class ValidatorChainTests: XCTestCase {
chain.validate(false, context: nil)
XCTAssertTrue(chain.errors.contains("Exception Thrown"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import XCTest
@testable import SwiftValidate

class ValidatorAlnumTests: XCTestCase {
class ValidatorCharsetTests: XCTestCase {

override func setUp() {
super.setUp()
Expand All @@ -25,6 +25,7 @@ class ValidatorAlnumTests: XCTestCase {

// code coverage
let _ = ValidatorAlnum()
let _ = ValidatorCharset()

let validator = ValidatorAlnum() {
$0.allowEmpty = true
Expand All @@ -51,8 +52,9 @@ class ValidatorAlnumTests: XCTestCase {

func testValidatorCanHandleNil() {

let validator = ValidatorAlnum() {
let validator = ValidatorCharset() {
$0.allowEmpty = true
$0.charset = NSCharacterSet.alphanumericCharacterSet()
}

var result: Bool = true
Expand All @@ -69,10 +71,28 @@ class ValidatorAlnumTests: XCTestCase {
}
}

func testValidatorThrowsIfNoCharsetIsGiven() {

let validator = ValidatorCharset() {
$0.allowEmpty = true
}

do {

try validator.validate(true, context: nil)
XCTAssert(false, "may never be reached")

} catch let error as NSError {

XCTAssertEqual("no chaset given", error.localizedDescription)
}
}

func testValidatorThrowsOnIllegalInput() {

let validator = ValidatorAlnum() {
let validator = ValidatorCharset() {
$0.allowEmpty = true
$0.charset = NSCharacterSet.alphanumericCharacterSet()
}

do {
Expand Down

0 comments on commit 836e1d7

Please sign in to comment.