ValidatorT

public class ValidatorT<S, T>

A class representing validations that can fail with one or multiple errors and can transforma data in the process.

  • Undocumented

    Declaration

    Swift

    public init(_ validation: @escaping ValidationT<S, T>)
  • Undocumented

    Declaration

    Swift

    public func callAsFunction(_ subject: S) -> Result<T, ValidationErrors>
  • Creates a validator that always succeeds

    List a pure function into the context of a Validator.

    Declaration

    Swift

    public static func lift(_ function: @escaping (S) -> T) -> ValidatorT

    Parameters

    value

    The value with which this validator should succeed.

  • Transforms the output of the validator i.e the subject/state being validated.

    Declaration

    Swift

    public func map<O>(_ transform: @escaping (T) -> O) -> ValidatorT<S, O>

    Parameters

    transform

    A function that can transform the resulting ouput of the validator.

    Return Value

    A validator with the same validation logic of caller but with a potentially different output type and value.

Utilities

  • Calls the validator with a provided subject

    Declaration

    Swift

    public func verify(_ subject: S) -> Result<T, ValidationErrors>
  • Runs the validator and collects errors

    Declaration

    Swift

    public func errors(_ subject: S) -> [Error]
  • Runs the validator collecting errors of specific type

    Declaration

    Swift

    public func errors<CustomError>(_ subject: S, errorType: CustomError.Type) -> [CustomError] where CustomError : Error
  • Runs the validator and collects error, such that they are arranged by a field type. This is useful for forms

    - Parameter fieldSelector: A function that will convert an error to a specific field
    

    Declaration

    Swift

    public func groupedErrors<ErrorType, Field>(_ subject: S, by fieldSelector: (ErrorType) -> Field) -> [Field : [ErrorType]] where Field : Hashable

Composition

  • Creates a new validator from caller and supplied validator

    Implementation of flatMap for Validator class. Will only run the provided validator if the caller succeeds

    Declaration

    Swift

    public func andThen<O>(_ check: ValidatorT<T, O>) -> ValidatorT<S, O>

    Parameters

    check

    A validator to compose

    Return Value

    A validators that either succeeds or fails with a single error.

  • Chains a parallel validator to caller

    Declaration

    Swift

    public func add<T2, O>(_ validator: ValidatorT<S, T2>, merge: @escaping (T, T2) -> O)
        -> ValidatorT<S, O>

    Parameters

    merge

    A function to merge or select the final output when both validation succeed.

    Return Value

    A validator that will run the caller validation in parallel with the supplied validator.

Utilities

  • Convert validator to a validator on optionals

    Declaration

    Swift

    public func optional() -> ValidatorT<S?, T?>
  • Adds a check to the caller Validator

    Declaration

    Swift

    public func addCheck(_ check: @escaping Predicate<S>, otherwise error: Error) -> ValidatorT

    Parameters

    check

    The predicate conforming the agregate validator.

    otherwise

    The error added to the error list of the caller.

  • Chains a validator created from a predicate

    Note the validator will only run after the current validator succeeds

    This is a shorthand for andThen(Verify.that(...))

    Declaration

    Swift

    public func andThat(_ predicate: @escaping Predicate<S>, otherwise failure: Error) -> ValidatorT

    Parameters

    predicate

    A predicate to test when the calling validator succeeds

    failure

    The error associated with this validator

  • Chaings a validation created from the provided predicate to the caller.

    Declaration

    Swift

    public func thenCheck(_ predicate: @escaping Predicate<S>, otherwise failure: Error)
        -> ValidatorT

    Parameters

    predicate

    The predicate to check

    Return Value

    Validators that will run a sequential check when the caller succeeds.

  • Chains a validator that acts on a subfield of the current subject.

    Declaration

    Swift

    public func thenOn<F>(_ keyPath: KeyPath<S, F>, check validator: Validator<F>) -> Validator<S>

    Parameters

    keyPath

    The property keypath to focus on

    check

    The validator to apply

    Return Value

    Validators that includes checks from caller and the validation on child property.

Available where S == T

  • Bypass the validator when a certain condition holds true

    Declaration

    Swift

    public func ignore(when predicate: @escaping Predicate<S>) -> ValidatorT

    Parameters

    predicate

    A predicate determining if should bypass or not