PredicateConstraint

public struct PredicateConstraint<T, E> : Constraint where E : Error

A Constraint that links a Predicate to an Error that describes why the predicate evaluation has failed.

let constraint = PredicateConstraint(.email, error: EmailFailure.invalid)
let result = constraint.evaluate(with: "hello@nsagora.com)
  • Declaration

    Swift

    public typealias InputType = T
  • Declaration

    Swift

    public typealias ErrorType = E
  • Returns a new PredicateConstraint instance.

    let constraint = PredicateConstraint(.email, error: EmailFailure.invalid)
    let result = constraint.evaluate(with: "hello@nsagora.com)
    

    Declaration

    Swift

    public init(_ predicate: some Predicate<T>, error: E)

    Parameters

    predicate

    A Predicate to describes the evaluation rule.

    error

    An Error that describes why the evaluation has failed.

  • Returns a new PredicateConstraint instance.

    let constraint = PredicateConstraint(.email) {
       EmailFailure.invalidFormat($0)
    }
    let result = constraint.evaluate(with: "hello@nsagora.com)
    

    Declaration

    Swift

    public init(_ predicate: some Predicate<T>, errorBuilder: @escaping (T) -> E)

    Parameters

    predicate

    A Predicate to describes the evaluation rule.

    error

    A generic closure that dynamically builds an Error to describe why the evaluation has failed.

  • Returns a new PredicateConstraint instance.

    let constraint = PredicateConstraint(.email) {
       EmailFailure.invalid
    }
    let result = constraint.evaluate(with: "hello@nsagora.com)
    

    Declaration

    Swift

    public init(_ predicate: some Predicate<T>, errorBuilder: @escaping () -> E)

    Parameters

    predicate

    A Predicate to describes the evaluation rule.

    error

    A generic closure that dynamically builds an Error to describe why the evaluation has failed.

  • Returns a new PredicateConstraint instance.

    let constraint = PredicateConstraint {
       .email
    } errorBuilder: {
       EmailFailure.invalidFormat($0)
    }
    let result = constraint.evaluate(with: "hello@nsagora.com)
    

    Declaration

    Swift

    public init(_ predicateBuilder: @escaping () -> some Predicate<T>, errorBuilder: @escaping (T) -> E)

    Parameters

    predicateBuilder

    A a closure that dynamically builds a Predicate to describes the evaluation rule.

    error

    A generic closure that dynamically builds an Error to describe why the evaluation has failed.

  • Returns a new PredicateConstraint instance.

    let constraint = PredicateConstraint {
       .email
    } errorBuilder: {
       EmailFailure.invalid
    }
    let result = constraint.evaluate(with: "hello@nsagora.com)
    

    Declaration

    Swift

    public init(_ predicateBuilder: @escaping () -> some Predicate<T>, errorBuilder: @escaping () -> E)

    Parameters

    predicateBuilder

    A a closure that dynamically builds a Predicate to describes the evaluation rule.

    error

    A generic closure that dynamically builds an Error to describe why the evaluation has failed.

  • Evaluates the input on the provided Predicate.

    Declaration

    Swift

    public func evaluate(with input: T) -> Result<Void, Summary<E>>

    Parameters

    input

    The input to be validated.

    Return Value

    .success if the input is valid,.failure containing the Summary of the failing Constraints otherwise.