Latest 0.0.9
Homepage https://github.com/vsujan92/SVValidator
License MIT
Platforms ios 9.0
Authors ,

A validator to validate form inputs.

Installation

Cocoapods

pod 'SVValidator'

Carthage

github 'vsujan92/SVValidator'

Features

1) Characterset validation
2) Empty string validation
3) Empty space at beginning validation
4) Quantity limit validation
5) Quantity precision validation
6) Regex validation
7) String length validation

Technical Documentation

Validate characterset

public protocol CharacterSetValidator: Validator {
  var characterCase: CharacterSet { get }
  var error: Error { get }
}

public extension CharacterSetValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let stringValue = value as? String else { return .error(nil) }
    return stringValue.rangeOfCharacter(from: characterCase) != nil ?
      .ok(value) :
      .error(error)
  }

}

public protocol CharacterSetExclusiveValidator: Validator {
  var characterCase: CharacterSet { get }
  var error: Error { get }
}

public extension CharacterSetExclusiveValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let stringValue = value as? String else { return .error(nil) }
    return stringValue.rangeOfCharacter(from: characterCase) == nil ?
      .ok(value) :
      .error(error)
  }

}

Validate empty string

public protocol EmptyValidator: Validator {
  var error: Error { get }
}

public extension EmptyValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let stringValue = value as? String else { return .error(nil) }
    return stringValue.isEmpty ? .error(error) : .ok(value)
  }
}

Validate space at the beginning

public protocol EmptySpaceValidator: Validator {
  var error: Error { get }
}

public extension EmptySpaceValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let stringValue = value as? String else { return .error(nil) }
    return !stringValue.hasEmptySpaceAtBeg ?
      .ok(value) :
      .error(self.error)
  }
}

Validate quantity limit

public protocol FloatLimitValidator: Validator {
  var minQuantity: Float { get }
  var maxQuantity: Float { get }
  var error: Error { get }
}

public extension FloatLimitValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let floatValue = value as? Float else { return .error(nil) }
    return floatValue >= minQuantity && floatValue <= maxQuantity ?
      .ok(value) :
      .error(error)
  }
}

Validate quantity precision

public protocol FloatPrecisionValidator: Validator {
  var precisionLength: Int { get }
  var error: Error { get }
}

public extension FloatPrecisionValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let floatValue = value as? Float else { return .error(nil) }
    return floatValue.precisionCount <= precisionLength ?
      .ok(value) :
      .error(error)
  }
}

Validate string against regex

public protocol RegexValidator: Validator {
  var REGEX: String { get }
  var error: Error { get }
}

public extension RegexValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    let test = NSPredicate(format:"SELF MATCHES %@", self.REGEX)
    guard let stringValue = value as? String else { return .error(nil) }
    return test.evaluate(with: stringValue) ?
      .ok(value) :
      .error(self.error)

  }
}

Validate string length

public protocol StringLengthValidator: Validator {
  var minLength: Int { get }
  var maxLength: Int { get }
  var error: Error { get }
}

public extension StringLengthValidator {
  func validate<T>(_ value: T) -> ValidationResult<T> {
    guard let stringValue = value as? String else { return .error(nil) }
    return stringValue.characters.count >= minLength && stringValue.characters.count <= maxLength ?
      .ok(value) :
      .error(error)
  }
}

Example

Email validation

import UIKit

enum EmailValidationError: Error {
  case empty
  case invalidFormat
}

let EMAIL_REGEX = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"

extension EmailValidationError: LocalizedError {
  public var errorDescription: String? {
    switch self {
    case .empty: return NSLocalizedString("Email field cannot be empty", comment: "Empty email")
    case .invalidFormat: return NSLocalizedString("Email format is not valid", comment: "Invalid format")
    }
  }
}

class EmailEmptyValidator: EmptyValidator {
  let error: Error

  init(error: Error = EmailValidationError.empty) {
    self.error = error
  }

}

class EmailFormatValidator: RegexValidator {
  let REGEX: String
  let error: Error

  init(regex: String = EMAIL_REGEX, error: Error = EmailValidationError.invalidFormat) {
    self.REGEX = regex
    self.error = error
  }

}

class EmailValidator: CompositeValidator {
  let validators: [Validator]

  init(regex: String = EMAIL_REGEX) {
    self.validators = [
      EmailEmptyValidator(),
      EmailFormatValidator(regex: regex)
    ]
  }
}

let emailValidator = EmailValidator()
let emailValue = "[email protected]"
emailValidator.validate(emailValue)

Credit

This project is highly inspired from this link.

License

SVValidator is released under MIT license.

Latest podspec

{
    "name": "SVValidator",
    "version": "0.0.9",
    "summary": "Validator to validate form input.",
    "homepage": "https://github.com/vsujan92/SVValidator",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "vsujan": "[email protected]",
        "vsujan92": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/vsujan92/SVValidator.git",
        "tag": "0.0.9"
    },
    "source_files": [
        "SVValidator/Extension/*.{swift}",
        "SVValidator/Util/Validator/*.{swift}"
    ],
    "exclude_files": "SVValidator/Extension/Textfield+Extension.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This