Giter VIP home page Giter VIP logo

genericvalidator's Introduction

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.

genericvalidator's People

Contributors

klein-thibault avatar thibaultklein avatar

Watchers

Carabineiro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.