-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dtop/feature/rc6
Feature/rc6
- Loading branch information
Showing
9 changed files
with
143 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters