GenericValidator provides helper functions and protocols to help you validate any kind of data that conforms to the Evaluatable
and Validatable
protocols.
The framework provides UITextField
and String
extensions that you can directly use to validate their content.
You can also conform to the Evaluatable
and Validatable
protocols in your custom types to provide a more custom validation to your code.
For examples, see the Usage section.
This framework was inspired by these blog articles:
- iOS 9+
- Xcode 8
- Swift 3+
To use in your projects, simply add the following line to your Podfile:
pod 'GenericValidator'
You can then use GenericValidator by importing it into your files:
import GenericValidator
You can use the validate function provided in the UITextField
or String
extensions:
func isCVCValid(text: String) -> Bool {
let regexp = "^[0-9]{3,4}$"
return text.evaluate(with: regexp)
}
let cvcTextField = UITextField()
cvcTextField.text = "123"
cvcTextField.validate([isCVCValid])
You can also conform to the protocols with your custom types:
struct User {
let firstName: String
let lastName: String
let age: Int
}
extension User: Validatable {
func validate(_ functions: [(User) -> Bool]) -> Bool {
return functions.map { f in f(self) }.reduce(true) { $0 && $1 }
}
}
func isUserNameValid(user: User) -> Bool {
let regexp = "[A-Za-z] "
return user.firstName.evaluate(with: regexp) && user.lastName.evaluate(with: regexp)
}
func isUserAdult(user: User) -> Bool {
return user.age >= 18
}
let user = User(firstName: "Thibault", lastName: "Klein", age: 25)
XCTAssertTrue(user.validate([isUserNameValid, isUserAdult]))
To report a bug or enhancement request, feel free to file an issue under the respective heading.
If you wish to contribute to the project, fork this repo and submit a pull request. Code contributions should follow the standards specified in the Prolific Swift Style Guide.
Copyright (c) 2016 Prolific Interactive
GenericValidator is maintained and sponsored by Prolific Interactive. It may be redistributed under the terms specified in the LICENSE file.