Protocols
The following protocols are available globally.
-
A type-erased validation type.
This protocol uses
See moreAny
instead of an associated type for thevalidation
method paramater. It also uses atype
property to store what the expected input type is.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 aFailable
type ifFailable.T
andValidation.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
See moresubvalidations
property and thevalidate
method have default implementations, so neither need to be implemented to conform toValidation
.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
See morevalidate
method has a length the is greater thanmaxLength
,ValidationError.lengthTooLong
is thrown. If the value has a length less thanminLength
,ValidationError.lengthTooShort
is thrown.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 themin
value,ValidationError.valueTooSmall
is thrown.If the
See moremax
ormin
value isnil
, 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.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,
See moreValidationError.noRegexMatch
will be thrown.Declaration
Swift
public protocol RegexValidation: Validation where Supported == String