Skip to content
/ Valy Public

Valy is string validation library with Swift 4.

License

Notifications You must be signed in to change notification settings

fromkk/Valy

Repository files navigation

Carthage compatible Build Status

Valy

Valy is string validation library with Swift 5.

Required

  • Swift 5
  • Xcode 10
  • iOS 8~ / watchOS 2~ / tvOS 8~ / macOS 10.16~
  • Carthage or Cocoapods

If you use Swift 4.2 or before, try Valy 0.0.6.

Install

with Carthage

Add github "fromkk/Valy" to Cartfile and execute carthage update command on your terminal in project directory.

Add Carthage/Build/{Platform}/Valy.framework to Link Binary with Libralies in you project.
If you doesn't use Carthage, add New Run Script Phase and input /usr/local/bin/carthage copy-frameworks in Build Phases tab.
Add $(SRCROOT)/Carthage/Build/{Platform}/Valy.framework to Input Files.

with Cocoapods

Add pod 'Valy' to Podfile and run pod install command on your terminal in project directory. Open {YourProject}.xcworkspace file.

Usage

Basic

import Valy

let value: String? = nil
let result = Valy.factory(rules: [ValyRule.required]).run(with: value)
switch result {
case .failure(let rule):
    switch rule {
    case ValyRule.required:
        print("value (\(value)) is required...")
    default:
        break
    }
default:
    print("value (\(value)) is successed!")
    break
}

Add rule

Valy.factory().add(rule: ValyRule.required).add(rule: ValyRule.maxLength(10)).run(with: value)

Text Field

let textField = VLTextField(frame: frame)
textField.add(rules: [
    ValyRule.required,
    ValyRule.maxLength(30)
])
textField.ovserveValidation { (status, result) in
    switch result {
    case .success:
        print("textField success")
    case .failure(let rule):
        print("failed \(rule)")
    }
}

CustomRule

enum CustomValidatorRule: AnyValidatorRule {
    case email
    func run(with value: String?) -> Bool {
        switch self {
        case .email:
            return doYourEmailValidation()
        }
    }
}

Valy.factory().add(rule: CustomValidatorRule.email).run(with: value)