<commit_before>Swift-validator
===============

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

Core Concepts

* ```UITextField``` + ```ValidationRule``` go into  ```Validator```
* ```UITextFields``` + ```ValidationError``` come out of ```Validator```
* ```UITextField``` is registered to ```Validator```
* ```Validator``` evaluates ```ValidationRules``` sequentially and stops evaluating when a ```ValidationRule``` fails. 

## Quick Start

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

```swift

// ViewController.swift

override func viewDidLoad() {
        super.viewDidLoad()
        
        var validator = Validator(delegate: self)
}

```

Register the fields that you want to validate

```swift

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.registerField(fields[0], textField:nameTextField, rules: [.Required, .FullName])
validator.registerField(fields[1], textField:emailTextField, rules: [.Required, .Email])
validator.registerField(fields[2], textField:phoneTextField, rules: [.Required, .PhoneNumber])

```

Validate Individual Field

```swift

func validator.validateFieldBy(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

```swift

validator.validateAllBy(fields, delegate:self)

// ValidationDelegate methods

func validationWasSuccessful(){
	// submit the form
}

func validationFailed(key:String, 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

```swift

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
        }
        return (false, .SocialSecurity)
    }
    
}

```

Add the ```.SocialSecurity``` ValidationRuleType

```swift

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

```

Add the ```.SocialSecurity``` ValidationErrorType and description()

```swift

enum ValidationErrorType {
    case Required,
    Email,
    Password,
    MinLength,
    MaxLength,
    ZipCode,
    PhoneNumber,
    FullName,
    SocialSecurity,	// Added to the Error Types
    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 Validation with the ValidationFactory

```swift

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()<commit_msg>updating readme 3<commit_after>Swift-validator
===============

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

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. 

## Quick Start

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

```swift

// ViewController.swift

override func viewDidLoad() {
        super.viewDidLoad()
        
        var validator = Validator(delegate: self)
}

```

Register the fields that you want to validate

```swift

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.registerField(fields[0], textField:nameTextField, rules: [.Required, .FullName])
validator.registerField(fields[1], textField:emailTextField, rules: [.Required, .Email])
validator.registerField(fields[2], textField:phoneTextField, rules: [.Required, .PhoneNumber])

```

Validate Individual Field

```swift

func validator.validateFieldBy(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

```swift

validator.validateAllBy(fields, delegate:self)

// ValidationDelegate methods

func validationWasSuccessful(){
	// submit the form
}

func validationFailed(key:String, 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

```swift

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``` 

```swift

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

```

Add the ```ValidationErrorType.SocialSecurity``` and ```description()```

```swift

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 <PASSWORD>"
        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```

```swift

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()
{'fraction_non_alphanumeric': 0.10703205791106515, 'fraction_numerical': 0.0023784901758014476, 'mean_word_length': 2.9521863506334287, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6732191', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2585, 'n_words': 817}