Validation
public protocol Validation: 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
.
-
The type that this validation can validate.
Declaration
Swift
associatedtype Supported
-
validate(_:)
Default implementationA type-safe method for validating a new value for a
Failable
instance.Default Implementation
The default implementation of the
Validation.validate
method. Does nothing. You should keepo the default implementation if the validation is just a collection of other validations to run.The default implementation for the type-erased
validate
method. This implementation takes the value and trys to cast it to the associatedSupport
type. If casting fails,ValidationError.invalidType
is thrown. Otherwise, the type-safevalidate
method is called.Declaration
Swift
static func validate(_ value: Supported)throws
Parameters
value
The new value to validate.
-
type
Extension methodDeclaration
Swift
public static var type: Any.Type
-
subvalidations
Extension methodDeclaration
Swift
public static var subvalidations: [AnyValidation.Type]
-
safeSubvalidations
Extension methodSubvalidations that expect an input type that matches the validation’s
Support
type.Declaration
Swift
public static var safeSubvalidations: [AnyValidation.Type]
-
run(_:)
Extension method