A rule-based validation library for Swift

We all have heard of the need for validation in Swift. The problem is that there are no good libraries to do so, which means you’re limited to writing your own code! Enter rules-based validation library RulesKit by Niklas Wiström. This library will help keep every part of your app clean and easy to maintain because it handles validations on its own without any subclasses or boilerplate code.

The “form swift github” is a rule-based validation library for Swift. It can be used to validate user input from forms, text fields, and other places in your app.

Swift Validator is a Swift-based rule-based validation framework.

Fundamental Ideas

  • Validator contains UITextField + [Rule] + (and optional error UILabel).
  • Validator returns UITextField + ValidationError.
  • Validator analyzes [Rule] in order and quits when one of them fails.

Installation

platform:ios, “8.1” use frameworks! # Podfile source ‘https://github.com/CocoaPods/Specs.git’ # :git => ‘https://github.com/jpotts18/SwiftValidator.git’, :tag => ‘4.2.0’ Swift 4.2 pod ‘SwiftValidator’, :tag => ‘4.2.0’ ‘SwiftValidator’,:git => ‘https://github.com/jpotts18/SwiftValidator.git’,:branch =>’master’,:git => ‘https://github.com/jpotts18/SwiftValidator.git’,:branch =>’master’,:branch =>’master’,:branch => ‘ UITextField has been extended beyond Swift 2.1. # ‘SwiftValidator’,:git => ‘https://github.com/jpotts18/SwiftValidator.git’,:tag => ‘4.0.0’,:tag => ‘SwiftValidator.git’,:tag => ‘SwiftValidator.git’,:tag => ‘SwiftValidator.git’,:tag => ‘SwiftValidator’,:git => ‘https://github.com/jpotts18/SwiftValidator.git’,:tag => ‘3.0.5’ # Swift 2.1 (limited to UITextField validation) pod ‘SwiftValidator’,:git => ‘https://github.com/jpotts18/SwiftValidator.git’,

Incorporate the following into your project:

$ install pod

Open your project in Xcode by double-clicking the.xcworkspace file (rather than the standard project file):

MyProject.xcworkspace is now open.

You’ll need to include this to your Cartfile if you’re using Carthage.

“jpotts18/SwiftValidator” on github

Usage

The SwiftValidator framework may now be imported into your projects.

Set a delegate to a View Controller or another object to start the Validator.

/ Validator = Validator in ViewController.swift ()

Make a list of the fields you wish to verify.

override the function super.viewDidLoad() super.viewDidLoad() super.viewDidLoad() super.viewDidLoad() super.viewDi () / Validation Rules are assessed in a clockwise direction. validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()]) validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()]) validator.registerField( With your rules, you may pass in error labels. / Custom error messages may be sent to the validator for regex rules (such as ZipCodeRule and EmailRule). emailTextField, emailErrorLabel) registerField(emailTextField, emailErrorLabel) registerField(emailText [RequiredRule(), EmailRule(message: “Invalid email”)]) ErrorLabel, rules: [RequiredRule(), EmailRule(message: “Invalid email”)]) The ConfirmRule validator may be used to verify against additional fields. registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)]) / You can now use the overloaded constructors validator to pass in regex and length arguments. phoneNumberTextField, phoneErrorLabel: phone) registerField(phoneNumberTextField, phoneErrorLabel: phone) Validator with NumberErrorLabel and [RequiredRule(), MinLengthRule(length: 9)] rules. registerField(zipcodeTextField, zipcodeErrorLabel) [RequiredRule(), ZipCodeRule(regex: “d5″”)]) ErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex: “d5″”)]) If you no longer want a text field to be validated by the validator, you may unregister it. unregisterField(fullNameTextField) unregisterField(fullNameTextField) unregisterField(fullNameTextFi

Validate fields when a button is pressed or whenever you want.

func signup @IBAction @IBAction @IBAction @IBAction @IBA validator.validate(self) tapped(sender: AnyObject) tapped(sender: AnyObject) tapped(sender: AnyObject) tapped(sender: AnyObject) tapped(sender

In your View controller, use the Validation Delegate.

/ Verification Methods to delegate Validation of functions / submit the form with success() Validation of functions Failed( errors: [(Validatable,ValidationError)]) [(Validatable,ValidationError)]) [(Validatable,Val / For (field, error) in errors, make the fields red. if let field = field = field as? field.layer.border UITextField UI = color Color.red.cg field.layer.borderWidth = 1.0 color field.layer.borderWidth = 1.0 color field.layer.borderWidth = If you added labels, error.errorLabel?.text = error.errorMessage isHidden = false error.errorLabel?

Validation of a single field

In certain circumstances, single field validation may be used. This might be handy in instances when you need to keep track of responders:

Remember to use UITextFieldDelegate in view and delegate yourTextField to self. textFieldShouldReturn(textField: UITextField) -> DidLoad() func textFieldShouldReturn(textField: UITextField) -> if error == nil / Field validation was successful else / Validation error occurred return true validator.validateField(textField) error in Bool validator.validateField(textField) error in Bool validator.validateField(textField) error in Bool validator.validateField(textField) error in Bool validator.validateField(textField) error in Bool validator.validateField

Validation that is unique to you

To demonstrate how to design your own Validation, we’ll develop an SSNRule class. The field XXX-XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Create a class that is based on RegexRule.

class SSNVRule: RegexRule static let regex = “d3-d2-d4$” convenience init(message: String = “Not a valid SSN”) init(message: String = “Not a valid SSN”) init(message: String = “Not a valid SSN”) init(message: String = “Not a valid SSN”) init(message: String = “Not a valid SSN”) in self.init(SSNVRule.regex, message: message, regex: SSNVRule.regex)

Documentation

Visit @jazzydocs to see the docs.

Credits

Jeff Potter @jpotts18 created and maintains Swift Validator. David Patterson (@dave tw12) is an active collaborator. Deniz Adalar deserves special credit for extending validation beyond UITextField.

Contributing

  1. Fork it
  2. git checkout -b my-new-feature creates your feature branch
  3. Make your adjustments and save them. git commit -am “Add some functionality”
  4. git push origin my-new-feature git push origin my-new-feature git push origin my-new-feature git push origin my-
  5. Make a fresh Pull Request.
  6. Ensure that at least 70% of the code is covered.

GitHub

https://github.com/SwiftValidatorCommunity/SwiftValidator

This “phone number validation with country code in swift” is a rule-based validation library for Swift. It allows you to validate phone numbers and detect invalid input.

Related Tags

  • text field validation in swift 4
  • atgvalidator
  • eureka swift
  • ios input validation
  • validatedpropertykit