Giter Club home page Giter Club logo

chirrup's Introduction

Chirrup

Build Status codecov.io Carthage compatible

Chirrup is a value validator for Swift.

Available validation rules(or types)

  • IsTrue -- check if value is True
  • Greater(than: String) -- check if value is strictly greater than
  • Lower(than: String) -- check if value is strictly lower than
  • NonEmpty -- check if value is not empty, uses Swift's isEmpty
  • Between(from: String, to: String) -- check if value is between two other values. Uses Greater and Lower rules in its implementation
  • Contains(value: String) -- check if string contains the value. Case insensitive
  • IsNumeric -- check if value is numeric one. Tries to convert to Double?

Usage

First create the Chirrup instance

let chirrup = Chirrup()

Then the following methods are available for use:

public func validate(fieldName: String, value: String, with rules: [ValidationRule],
      _ callback: validationCallback? = nil) -> [ValidationRule]
public func validate(fieldName: String, value: String, with rule: ValidationRule,
      _ callback: validationCallbackSingle? = nil) -> ValidationRule?
  • fieldName: String is passed to callback if such is provided as the last argument
  • value to be validated
  • rules validation types to validate value with
  • and optional callback which runs no matter what if passed as an argument. It gets the errors array and the field name(field below in Examples section) or just one error - it depends on callback type:
  public typealias validationCallback       = (errors: [ValidationRule],  fieldName: String) -> ()
  public typealias validationCallbackSingle = (error:  ValidationRule?,   fieldName: String) -> ()

Examples:

chirrup.validate("Search field", value: sender.text!,
      with: [ValidationRule(.NonEmpty),
             ValidationRule(.Contains(value: "UberCar"))]) { errors, field in
        let errorMessages = self.chirrup.formatMessagesFor(field, from: errors)
        // errorMessages == "Search field should not be empty\nSearch field should contain `UberCar`"
        self.errorLabelSearchField.text = errorMessages
}

or instead of closure callback one can use errors returned by validate method

let errors = chirrup.validate("Search field", value: sender.text!,
      with: [ValidationRule(.NonEmpty),
             ValidationRule(.Contains(value: "UberCar"))])
expect(chirrup.formatMessagesFor(fieldName, from: errors))
            .to(equal("Search field should not be empty\nSearch field should contain `UberCar`"))

or call validate with single validation rule(one can use callback closure here as well)

let error = chirrup.validate("Activator", value: false,
    with: ValidationRule(.IsTrue))

expect(error!.errorMessageFor(fieldName))
    .to(equal("Activator should be true"))

validate return value

errors and error returned above are validation rules themselves and could be initialized with several options and have following constructor

public init(_ type: ValidationRuleType,
    message: String? = nil, on: (() -> Bool)? = nil)

where

  • type is one of rules described above
  • message:String? is optional and represents full error message, e.g. "This field should include 'hey there!' "
  • on is optional too and you can pass some code which returns Bool. In case it returns true the validation rule is evaluated otherwise skipped
let val = ""
let error = chirrup.validate(fieldName, value: val,
              with: ValidationRule(.Lower(than: "10000.00"), on: { !val.isEmpty }))

Above validation is skipped.

Installation

Add Chirrup to your Cartfile:

github "bilogub/Chirrup"

chirrup's People

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.