Latest 0.1.0
Homepage https://github.com/royhsu/tiny-validation
License MIT
Platforms ios 8.0
Frameworks Foundation
Authors

CocoaPods Compatible
Carthage compatible
Platform

A fluent and elegant validation framework for Swift.

Requirements

  • iOS 8.0+
  • Xcode 10.0+
  • Swift 4.0+

Installation

You can install TinyValidation using CocoaPods or Carthage.

CocoaPods

use_frameworks!

pod 'TinyValidation'

Carthage

github 'royhsu/TinyValidation'

How to Use

Please make sure to import the framework.

import TinyValidation

Built-in Validation Rules

You can use the built-in rules for trivial validations.

Example: Validate an email.

let validEmail = try "[email protected]".validated(by: EmailRule.standard)

Currently supported rules.

  • Email

Custom Validation Rules

Create your custom rules by conforming to ValidationRule protocol.

Example: Validate a password.

// Custom password rules.
enum PasswordRule: ValidationRule {

    case minimumLength(Int)

    case maximumLength(Int)

    @discardableResult
    func validate(_ value: String) throws -> String {

        switch self {

        case let .minimumLength(length):

            if value.count < length { throw PasswordError.minimumLength(length) }

            return value

        case let .maximumLength(length):

            if value.count > length { throw PasswordError.maximumLength(length) }

            return value

        }

    }

}

Then you can validate multiple rules at once.

let passwordRules: [PasswordRule] = [
    .minimumLength(8),
    .maximumLength(10)
]

let validPassword = try "12345678".validated(by: passwordRules)

Reference

Latest podspec

{
    "name": "TinyValidation",
    "version": "0.1.0",
    "license": "MIT",
    "summary": "A fluent and elegant validation framework for Swift.",
    "homepage": "https://github.com/royhsu/tiny-validation",
    "authors": {
        "Roy Hsu": "[email protected]"
    },
    "source": {
        "git": "https://github.com/royhsu/tiny-validation.git",
        "tag": "0.1.0"
    },
    "frameworks": "Foundation",
    "source_files": "Sources/*.swift",
    "platforms": {
        "ios": "8.0"
    },
    "swift_version": "4.2"
}

Pin It on Pinterest

Share This