Skip to content

carabina/GenericValidator

 
 

Repository files navigation

GenericValidator

Travis build status Cocoapods Compatible Platform Docs

Description

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:

Requirements

  • iOS 9+
  • Xcode 8
  • Swift 3+

Installation

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

Usage

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]))

Contributing to GenericValidator

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.

License

Prolific Interactive

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 75.6%
  • Ruby 24.4%