Giter Site home page Giter Site logo

swift-validator's Introduction

Swift-Validator

Swift Validator is a rule-based validation library for Swift.

Swift Validator

Core Concepts

  • UITextField + ValidationRule go into Validator
  • UITextField + ValidationError come out of Validator
  • UITextField is registered to Validator
  • Validator evaluates ValidationRules sequentially and stops evaluating when a ValidationRule fails.
  • Keys are used to allow field registration in TableViewControllers and complex view hierarchies

Quick Start

Initialize the Validator by setting a delegate to a View Controller or other object.

// ViewController.swift

let validator = Validator()

override func viewDidLoad() {
    super.viewDidLoad()
}

Register the fields that you want to validate

var fields:[String] = ["FullName", "Email", "Phone"]

// Validation Rules are evaluated from left to right. The first rule is ValidationRuleType.Required the second is ValidationRuleType.FullName.
validator.registerFieldByKey(fields[0], textField:nameTextField, rules: [.Required, .FullName])
validator.registerFieldByKey(fields[1], textField:emailTextField, rules: [.Required, .Email])
validator.registerFieldByKe(fields[2], textField:phoneTextField, rules: [.Required, .PhoneNumber])

Validate Individual Field

validator.validateFieldByKey(fields[0], delegate:self)

// ValidationFieldDelegate methods
func validationFieldSuccess(key:String, validField:UITextField){
	validField.backgroundColor = UIColor.greenColor()
}

func validationFieldFailure(key:String, error:ValidationError){
	println(error.error.description)
}

Validate All Fields

validator.validateAllKeys(delegate:self)

// ValidationDelegate methods

func validationWasSuccessful() {
	// submit the form
}

func validationFailed(errors:[String:ValidationError]){
	// turn the fields to red
	for error in errors.values {
		error.textField.backgroundColor = UIColor.redColor()
		println("error -> \(error.error.description)")
	}
}

Custom Validation

We will create a SSNValidation class to show how to create your own Validation. A United States Social Security Number (or SSN) is a field that consists of XXX-XX-XXXX.

Create a class that implements the Validation protocol

class SSNValidation: Validation {
    let SSN_REGEX = "^\\d{3}-\\d{2}-\\d{4}$"
    
    func validate(value: String) -> (Bool, ValidationErrorType) {
        if let ssnTest = NSPredicate(format: "SELF MATCHES %@", SSN_REGEX) {
            if ssnTest.evaluateWithObject(value) {
                return (true, .NoError)
            }
            return (false, .SocialSecurity) // We will create this later ValidationErrorType.SocialSecurity
        }
        return (false, .SocialSecurity)
    }
    
}

Add the ValidationRuleType.SocialSecurity

enum ValidationRuleType {
    case Required,
    Email,
    Password,
    MinLength,
    MaxLength,
    ZipCode,
    PhoneNumber,
    FullName,
    SocialSecurity	// Added to the ValidationRuleTypes
}

Add the ValidationErrorType.SocialSecurity and description()

enum ValidationErrorType {
    case Required,
    Email,
    Password,
    MinLength,
    MaxLength,
    ZipCode,
    PhoneNumber,
    FullName,
    SocialSecurity,	// Added to the ValidationErrorTypes
    NoError
    
    func description() -> String {
        switch self {
        case .Required:
            return "Required field"
        case .Email:
            return "Must be a valid email"
        case .MaxLength:
            return "This field should be less than"
        case .ZipCode:
            return "5 digit zipcode"
        case .PhoneNumber:
            return "10 digit phone number"
        case .Password:
            return "Must be at least 8 characters"
        case .FullName:
            return "Provide a first & last name"
        // Adding the desired error message
        case .SocialSecurity:
        	return "SSN is XXX-XX-XXXX"
        default:
            return ""
        }
    }
    
}

Register the SSNValidation with the ValidationFactory

class ValidationFactory {
    class func validationForRule(rule:ValidationRuleType) -> Validation {
        switch rule {
        case .Required:
            return RequiredValidation()
        case .Email:
            return EmailValidation()
        case .MinLength:
            return MinLengthValidation()
        case .MaxLength:
            return MaxLengthValidation()
        case .PhoneNumber:
            return PhoneNumberValidation()
        case .ZipCode:
            return ZipCodeValidation()
        case .FullName:
            return FullNameValidation()
        // Add Validation to allow Factory to create one on the fly for you
        case .SocialSecurity:
        	return SSNValidation()
        default:
            return RequiredValidation()
        }
    }
}

Credits

Swift Validator is written and maintained by Jeff Potter @jpotts18 and friends.

Currently funded and maintained by RingSeven

RingSeven

Contributing

  1. Fork it
  2. Create your feature branch git checkout -b my-new-feature
  3. Commit your changes git commit -am 'Add some feature'
  4. Push to the branch git push origin my-new-feature
  5. Create a new Pull Request

swift-validator's People

Contributors

jpotts18 avatar

Watchers

 avatar  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.