updated codebase to reflect current codebase
This commit is contained in:
parent
03a3a7ea2c
commit
0f8871426a
|
|
@ -14,11 +14,13 @@ import UIKit
|
|||
@objc public protocol ValidationDelegate {
|
||||
/**
|
||||
This method will be called on delegate object when validation is successful.
|
||||
|
||||
- returns: No return value.
|
||||
*/
|
||||
func validationSuccessful()
|
||||
/**
|
||||
This method will be called on delegate object when validation fails.
|
||||
|
||||
- returns: No return value.
|
||||
*/
|
||||
func validationFailed(errors: [UITextField:ValidationError])
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ public class ValidationError: NSObject {
|
|||
|
||||
/**
|
||||
Initializes `ValidationError` object with a textField and error.
|
||||
|
||||
- parameter textField: UITextField that holds textField.
|
||||
- parameter errorMessage: String that holds error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
@ -29,7 +30,8 @@ public class ValidationError: NSObject {
|
|||
}
|
||||
|
||||
/**
|
||||
Initializes ValidationError object with a textField, errorLabel, and errorMessage.
|
||||
Initializes `ValidationError` object with a textField, errorLabel, and errorMessage.
|
||||
|
||||
- parameter textField: UITextField that holds textField.
|
||||
- parameter errorLabel: UILabel that holds error label.
|
||||
- parameter errorMessage: String that holds error message.
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ public class Validator {
|
|||
/**
|
||||
This method is used to validate all fields registered to Validator. If validation is unsuccessful,
|
||||
field gets added to errors dictionary.
|
||||
|
||||
- returns: No return value.
|
||||
*/
|
||||
private func validateAllFields() {
|
||||
|
|
@ -58,6 +59,7 @@ public class Validator {
|
|||
/**
|
||||
This method is used to validate a single field registered to Validator. If validation is unsuccessful,
|
||||
field gets added to errors dictionary.
|
||||
|
||||
- parameter textField: Holds validator field data.
|
||||
- returns: No return value.
|
||||
*/
|
||||
|
|
@ -149,7 +151,6 @@ public class Validator {
|
|||
This method validates all fields in validator and sets any errors to errors parameter of callback.
|
||||
|
||||
- parameter callback: A closure which is called with errors, a dictionary of type UITextField:ValidationError.
|
||||
|
||||
- returns: No return value.
|
||||
*/
|
||||
public func validate(callback:(errors:[UITextField:ValidationError])->Void) -> Void {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,18 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
`AlphaNumericRule` is a subclass of `CharacterSetRule`. It is used to verify that a field has a
|
||||
valid list of alphanumeric characters.
|
||||
*/
|
||||
public class AlphaNumericRule: CharacterSetRule {
|
||||
|
||||
/**
|
||||
Initializes a `AlphaNumericRule` object to verify that field has valid set of alphanumeric characters.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(message: String = "Enter valid numeric characters") {
|
||||
super.init(characterSet: NSCharacterSet.alphanumericCharacterSet(), message: message)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,18 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
`AlphaRule` is a subclass of `CharacterSetRule`. It is used to verify that a field has a
|
||||
valid list of alpha characters.
|
||||
*/
|
||||
public class AlphaRule: CharacterSetRule {
|
||||
|
||||
/**
|
||||
Initializes an `AlphaRule` object to verify that a field has valid set of alpha characters.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(message: String = "Enter valid alphabetic characters") {
|
||||
super.init(characterSet: NSCharacterSet.letterCharacterSet(), message: message)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,16 +8,33 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
`CharacterSetRule` is a subclass of `Rule`. It is used to validate IPV4 address fields.
|
||||
*/
|
||||
public class CharacterSetRule: Rule {
|
||||
|
||||
/// NSCharacter that hold set of valid characters to hold
|
||||
private let characterSet: NSCharacterSet
|
||||
/// String that holds error message
|
||||
private var message: String
|
||||
|
||||
/**
|
||||
Initializes a `CharacterSetRule` object to verify that field has valid set of characters.
|
||||
|
||||
- parameter characterSet: NSCharacterSet that holds group of valid characters.
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(characterSet: NSCharacterSet, message: String = "Enter valid alpha") {
|
||||
self.characterSet = characterSet
|
||||
self.message = message
|
||||
}
|
||||
|
||||
/**
|
||||
Used to validate field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
for uni in value.unicodeScalars {
|
||||
if !characterSet.longCharacterIsMember(uni.value) {
|
||||
|
|
@ -27,6 +44,11 @@ public class CharacterSetRule: Rule {
|
|||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
Displays error message when text field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,19 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
`HexColorRule` is a subclass of `RegexRule`. It is used to verify whether a field is a hexadecimal color.
|
||||
*/
|
||||
public class HexColorRule: RegexRule {
|
||||
/// Regular expression string that is used to verify hexadecimal
|
||||
static let regex = "^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$"
|
||||
|
||||
/**
|
||||
Initializes a `HexaColorRule` object to verify that field has text in hexadecimal color format.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(message: String = "Invalid regular expression") {
|
||||
super.init(regex: HexColorRule.regex, message: message)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,10 +8,19 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
`IPV4Rule` is a subclass of RegexRule that defines how a IPV4 address validated.
|
||||
*/
|
||||
public class IPV4Rule: RegexRule {
|
||||
|
||||
/// Regular expression string that is used to verify IPV4 address.
|
||||
static let regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
|
||||
|
||||
|
||||
/**
|
||||
Initializes a `IPV4Rule` object to verify that field has text is an IPV4Rule address.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(message: String = "Must be a valid IPV4 address") {
|
||||
super.init(regex: IPV4Rule.regex, message: message)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,14 +8,30 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
`ISBNRule` is a subclass of `Rule`. It is used to verify whether a field is a valid ISBN number.
|
||||
*/
|
||||
public class ISBNRule: Rule {
|
||||
|
||||
/// String that holds error message
|
||||
private let message: String
|
||||
|
||||
/**
|
||||
Initializes a `ISBNRule` object to verify that field has text that is a ISBN number.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(message: String = "Enter valid ISBN number") {
|
||||
self.message = message
|
||||
}
|
||||
|
||||
/**
|
||||
Method used to validate text field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
|
||||
guard let regex = try? NSRegularExpression(pattern: "[\\s-]", options: []) else {
|
||||
|
|
@ -27,25 +43,75 @@ public class ISBNRule: Rule {
|
|||
return ISBN10Validator().verify(sanitized) || ISBN13Validator().verify(sanitized)
|
||||
}
|
||||
|
||||
/**
|
||||
Method used to dispaly error message when text field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
`ISBNValidator` defines the protocol that objects adopting it must implement.
|
||||
*/
|
||||
private protocol ISBNValidator {
|
||||
/// Regular expression string
|
||||
var regex: String { get }
|
||||
|
||||
/**
|
||||
Method that actually verifies a ISBN number.
|
||||
|
||||
- parameter input: String that is to be validated against `ISBNRule`
|
||||
- returns: A `Bool` that represents what happened during verification. `false` is returned if
|
||||
it fails, `true` is returned if it was a success.
|
||||
*/
|
||||
func verify(input: String) -> Bool
|
||||
/**
|
||||
Method that verifies regular expression is valid.
|
||||
|
||||
- parameter input: String that holds ISBN number being validated.
|
||||
- returns: A `Bool` that represents the status of the ISBN number. `false` if ISBN number
|
||||
was not valid, `true` if it was valid.
|
||||
*/
|
||||
func checkRegex(input: String) -> Bool
|
||||
|
||||
/**
|
||||
Method that verifies `ISBN` being validated is itself valid. It has to be either ISBN10
|
||||
or ISBN13.
|
||||
|
||||
- parameter input: String that holds ISBN number being validated.
|
||||
- returns: A `Bool` that represents the status of the ISBN number. `false` if ISBN number
|
||||
was not valid, `true` if it was valid.
|
||||
*/
|
||||
func verifyChecksum(input: String) -> Bool
|
||||
}
|
||||
|
||||
/**
|
||||
`ISBNValidator` defines the extensions that are added to `ISBNValidator`.
|
||||
*/
|
||||
extension ISBNValidator {
|
||||
|
||||
/**
|
||||
Method that actually verifies a ISBN number.
|
||||
|
||||
- parameter input: String that is to be validated against `ISBNRule`
|
||||
- returns: A `Bool` that represents what happened during verification. `false` is returned if
|
||||
it fails, `true` is returned if it was a success.
|
||||
*/
|
||||
func verify(input: String) -> Bool {
|
||||
return checkRegex(input) && verifyChecksum(input)
|
||||
}
|
||||
|
||||
/**
|
||||
Method that verifies `ISBN` being validated is itself valid. It has to be either ISBN10
|
||||
or ISBN13.
|
||||
|
||||
- parameter input: String that holds ISBN number being validated.
|
||||
- returns: A `Bool` that represents the status of the ISBN number. `false` if ISBN number
|
||||
was not valid, `true` if it was valid.
|
||||
*/
|
||||
func checkRegex(input: String) -> Bool {
|
||||
guard let _ = input.rangeOfString(regex, options: [.RegularExpressionSearch, .AnchoredSearch]) else {
|
||||
return false
|
||||
|
|
@ -55,9 +121,21 @@ extension ISBNValidator {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
`ISBN10Validator` is a struct that adopts the `ISBNValidator` protocol. Used to validate that
|
||||
a field is an ISBN10 number.
|
||||
*/
|
||||
private struct ISBN10Validator: ISBNValidator {
|
||||
/// Regular expression used to validate ISBN10 number
|
||||
let regex = "^(?:[0-9]{9}X|[0-9]{10})$"
|
||||
|
||||
|
||||
/**
|
||||
Checks that a string is an ISBN10 number.
|
||||
|
||||
- parameter input: String that is checked for ISBN10 validation.
|
||||
- returns: `true` if string is a valid ISBN10 and `false` if it is not.
|
||||
*/
|
||||
private func verifyChecksum(input: String) -> Bool {
|
||||
var checksum = 0
|
||||
|
||||
|
|
@ -79,9 +157,20 @@ private struct ISBN10Validator: ISBNValidator {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
`ISBN13Validator` is a struct that adopts the `ISBNValidator` protocol. Used to validate that
|
||||
a field is an ISBN13 number.
|
||||
*/
|
||||
private struct ISBN13Validator: ISBNValidator {
|
||||
/// Regular expression used to validate ISBN13 number.
|
||||
let regex = "^(?:[0-9]{13})$"
|
||||
|
||||
/**
|
||||
Checks that a string is an ISBN13 number.
|
||||
|
||||
- parameter input: String that is checked for ISBN13 validation.
|
||||
- returns: `true` if string is a valid ISBN13 and `false` if it is not.
|
||||
*/
|
||||
private func verifyChecksum(input: String) -> Bool {
|
||||
let factor = [1, 3]
|
||||
var checksum = 0
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ public class MaxLengthRule: Rule {
|
|||
public init(){}
|
||||
|
||||
/**
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a text field
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a text field.
|
||||
|
||||
- parameter length: Maximum character length.
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ public class MinLengthRule: Rule {
|
|||
public init(){}
|
||||
|
||||
/**
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a text field
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a text field.
|
||||
|
||||
- parameter length: Minimum character length.
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized `MinLengthRule` object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ public class PhoneNumberRule: RegexRule {
|
|||
static let regex = "^\\d{10}$"
|
||||
|
||||
/**
|
||||
Method used to initialize `PhoneNumberRule` object.
|
||||
Initializes a `PhoneNumberRule` object. Used to validate that a field has a valid phone number.
|
||||
|
||||
- parameter message: Error message that is displayed if validation fails.
|
||||
- returns: An initialized `PasswordRule` object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ public class RequiredRule: Rule {
|
|||
private var message : String
|
||||
|
||||
/**
|
||||
Initializes `RequiredRule` object with error message. Used to validate a field that requires text.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized `RequiredRule` object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue