Protocols

The following protocols are available globally.

  • A type-erased validation type.

    This protocol uses Any instead of an associated type for the validation method paramater. It also uses a type property to store what the expected input type is.

    See more

    Declaration

    Swift

    public protocol AnyValidation
  • A type that can be used to validate a new value for a Failable type.

    A Validation type can only be used for a Failable type if Failable.T and Validation.Suuported types are the same.

    An example Validation implementation might look like this:

    struct Length1028<C>: Validation where C: Collection {
        static func validate(_ value: C)throws {
            guard value.count <= 1028 else { throw Error.toLong }
        }
    }
    

    Both the subvalidations property and the validate method have default implementations, so neither need to be implemented to conform to Validation.

    See more

    Declaration

    Swift

    public protocol Validation: AnyValidation
  • A validation type that verifies the length of a type conforming to Collection.

    internal struct LengthRange10To1028<C>: LengthValidation where C: Collection {
        typealias Supported = C
    
        static var maxLength: Int { return 1028 }
        static var minLength: Int { return  10 }
    }
    

    If the value passed into the validate method has a length the is greater than maxLength, ValidationError.lengthTooLong is thrown. If the value has a length less than minLength, ValidationError.lengthTooShort is thrown.

    See more

    Declaration

    Swift

    public protocol LengthValidation: Validation where Supported: Collection
  • A validation for checking that a value is greater or equal to a lesser value and less than or equal to a greater value.

    struct NumberThousand: InRangeValidation {
        static let max: Int? = 9_999
        static let min: Int? = 1_000
    }
    

    If the value being validated is greater than the max value, ValidationError.valueTooGreat is throw. If the value is less than the min value, ValidationError.valueTooSmall is thrown.

    If the max or min value is nil, then the value being validated will not be checked against it. This allows for one sided validation ranges, where you could, for example, have any number greater than 42.

    See more

    Declaration

    Swift

    public protocol InRangeValidation: Validation where Supported: Comparable
  • Checks that a String value contains a match to a regular expression pattern somewhere in it.

    struct USPhoneNumber: RegexValidation {
        static let pattern = "1?-?\\(?[0-9]{3}\\)?-?[0-9]{3}-?[0-9]{4}"
    }
    

    If you want to make sure the RegEx pattern matches the whole string, use the ^ and $ operators at the start and end of the pattern.

    If the string passed in to be validated does not contain a match for the RegEx pattern, ValidationError.noRegexMatch will be thrown.

    See more

    Declaration

    Swift

    public protocol RegexValidation: Validation where Supported == String