Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jpotts18/swift-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jpotts18 committed Mar 24, 2015
2 parents ff25465 + b6f8cd6 commit 699f1a1
Showing 1 changed file with 50 additions and 40 deletions.
90 changes: 50 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ Swift-Validator

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


![Swift Validator](/swift-validator-v2.gif)

## Core Concepts

* ``UITextField`` + ``ValidationRule`` go into ```Validator``
* ``ITextField`` + ``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
* ``UITextField`` + ``[Rule]`` + (and optional error ``UILabel``) go into ``Validator``
* ``UITextField`` + ``ValidationError`` come out of ```Validator``
* ``Validator`` evaluates ``[Rule]`` sequentially and stops evaluating when a ``Rule`` fails.

## Quick Start

Expand All @@ -23,38 +22,43 @@ Initialize the ``Validator`` by setting a delegate to a View Controller or other

let validator = Validator()

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

```

Register the fields that you want to validate

```swift
override func viewDidLoad() {
super.viewDidLoad()

// Validation Rules are evaluated from left to right.
validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()])

// You can pass in error labels with your rules
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])

// You can validate against other fields using ConfirmRule
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)])

// You can now pass in regex and length parameters through overloaded contructors
validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")])

// Validation Rules are evaluated from left to right.
validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()])

// You can pass in error labels with your rules
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])
}
```

// You can validate against other fields using ConfirmRule
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)])

// You can now pass in regex and length parameters through overloaded contructors
validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")])
Validate Fields on button tap or however you would like to trigger it.

```swift
@IBAction func signupTapped(sender: AnyObject) {
validator.validateAll(delegate:self)
}
```


Validate All Fields
Implement the Validation Delegate in your View controller

```swift

validator.validateAll(delegate:self)

// ValidationDelegate methods

func validationWasSuccessful() {
Expand All @@ -63,47 +67,53 @@ func validationWasSuccessful() {

func validationFailed(errors:[UITextField:ValidationError]) {
// turn the fields to red
for (field, error) in validator.errors {
field.layer.borderColor = UIColor.redColor().CGColor
field.layer.borderWidth = 1.0
error.errorLabel?.text = error.errorMessage // works if you added labels
error.errorLabel?.hidden = false
}
for (field, error) in validator.errors {
field.layer.borderColor = UIColor.redColor().CGColor
field.layer.borderWidth = 1.0
error.errorLabel?.text = error.errorMessage // works if you added labels
error.errorLabel?.hidden = false
}
}

```

## 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.
We will create a ```SSNRule``` 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
Create a class that implements the Rule protocol

```swift

class SSNVRule: Rule {
let SSN_REGEX = "^\\d{3}-\\d{2}-\\d{4}$"
let REGEX = "^\\d{3}-\\d{2}-\\d{4}$"

init(){}

// allow for custom variables to be passed
init(regex:String){
self.REGEX = regex
}

func validate(value: String) -> (Bool, ValidationErrorType) {
if let ssnTest = NSPredicate(format: "SELF MATCHES %@", SSN_REGEX) {
func validate(value: String) -> Bool {
if let ssnTest = NSPredicate(format: "SELF MATCHES %@", REGEX) {
if ssnTest.evaluateWithObject(value) {
return (true, .NoError)
return true
}
return (false, .SocialSecurity) // We will create this later ValidationErrorType.SocialSecurity
return false
}
return (false, .SocialSecurity)
}

func errorMessage() -> String{
return "Not a valid SSN"
}

}
```

Credits
-------

Swift Validator is written and maintained by Jeff Potter [@jpotts18](http://twitter.com/jpotts18) and friends.
Swift Validator is written and maintained by Jeff Potter [@jpotts18](http://twitter.com/jpotts18).

## Contributing

Expand Down

0 comments on commit 699f1a1

Please sign in to comment.