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 implementation

    A 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 associated Support type. If casting fails, ValidationError.invalidType is thrown. Otherwise, the type-safe validate method is called.

    Declaration

    Swift

    static func validate(_ value: Supported)throws

    Parameters

    value

    The new value to validate.

  • type Extension method

    Declaration

    Swift

    public static var type: Any.Type
  • subvalidations Extension method

    Declaration

    Swift

    public static var subvalidations: [AnyValidation.Type]
  • safeSubvalidations Extension method

    Subvalidations that expect an input type that matches the validation’s Support type.

    Declaration

    Swift

    public static var safeSubvalidations: [AnyValidation.Type]
  • run(_:) Extension method

    Runs the currenct validation and any subvalidations that support the Supported type. This runs recursively until the bottom of the validation tree is found.

    Declaration

    Swift

    public static func run(_ value: Supported)throws

    Parameters

    value

    The value to validate.