Validation is done using the apply function of a Validator.
You can create a Validator manually or you can use on of the already available via static functions in the Validator class.
A Validator‘s apply function accepts an input as a nullable value that conforms to the StringConvertible protocol. By default String, NSString, Int, Float, Double and Bool conform to StringConvertible.
You can specify the Validator‘s behaviour when it’s input is nil if you are using the static Validator function by setting the nilResponse parameter to either true or false. By default nilResponse is set to false.
Validator.exactLength(3).apply("abc") //returns true
Validator.exactLength(3).apply(true) //returns false (the string representation of true is 'true')
Validator.exactLength(3).apply(nil) //returns false since `nilResponse` is set to false by default
Validator.exactLength(3, nilResponse: true).apply(nil) //returns true since we set nilResponse to true
For more examples on how to call each validator you can look at the unit tests.
Logical Operators
You can combine operators using the logical AND, logical OR and Logical NOT operators ( &&, || and ! respectively).
let combinedANDValidator = Validator.required() && Validator.isTrue()
The combinedANDValidator will be true only when the value is not empty and "true"
let combinedORValidator = Validator.isTrue() || Validators.isFalse()
The combinedORValidator will be true if the value is "true" or "false", otherwise it will be false.
let reversedValidator = !Validator.isTrue()
The reversedValidator will be false when the value equals "true" and true for all other values.
Copyright (c) George Kaimakas gkaimakas@gmail.com
Permission is hereby granted, free of charge, to any person obtaining
acopy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Swift Validators
String validation for iOS.
Contents
ReactiveSwift + SwiftValidators
Want to use
SwiftValidators
withReactiveSwift
?SwiftValidatorsReactiveExtensions
provides a set of extensions that play well withValidatingProperty
.Installation
SwiftValidators is available on CocoaPods for iOS 9.0+, Xcode 8 and Swift 3.0
It is also available through SPM:
Walkthrough
Usage
Validation is done using the
apply
function of aValidator
. You can create aValidator
manually or you can use on of the already available via static functions in the Validator class.A
Validator
‘s apply function accepts an input as a nullable value that conforms to theStringConvertible
protocol. By defaultString
,NSString
,Int
,Float
,Double
andBool
conform toStringConvertible
.You can specify the
Validator
‘s behaviour when it’s input is nil if you are using the static Validator function by setting thenilResponse
parameter to either true or false. By default nilResponse is set to false.For more examples on how to call each validator you can look at the unit tests.
Logical Operators
You can combine operators using the logical
AND
, logicalOR
and LogicalNOT
operators ( &&, || and ! respectively).The
combinedANDValidator
will betrue
only when the value is not empty and"true"
The
combinedORValidator
will betrue
if the value is"true"
or"false"
, otherwise it will be false.The
reversedValidator
will befalse
when the value equals"true"
andtrue
for all other values.Available Validators
*FQDNOptions is a class that is used on isFQDN for configuration purposes. It can be instantiated like this:
License MIT