BlockConstraint
public struct BlockConstraint<T, E> : Constraint where E : Error
A Constraint
that links a custom validation closure to an Error
that describes why the evaluation has failed.
enum Failure: Error {
case notEven
}
let constraint = BlockConstraint<Int, Failure> {
$0 % 2 == 0
} errorBuilder: {
.notEven
}
let result = constraint.evaluate(with: 2)
-
Declaration
Swift
public typealias InputType = T
-
Declaration
Swift
public typealias ErrorType = E
-
Returns a new
BlockConstraint
instance.enum Failure: Error { case notEven }
let constraint = BlockConstraint<Int, Failure> { $0 % 2 == 0 } errorBuilder: { .notEven } let result = constraint.evaluate(with: 2)
Declaration
Swift
public init(_ evaluationBlock: @escaping (T) -> Bool, errorBuilder: @escaping () -> E)
Parameters
evaluationBlock
A closure describing a custom validation condition.
errorBuilder
A generic closure that dynamically builds an
Error
to describe why the evaluation has failed. -
Create a new
BlockConstraint
instance.enum Failure: Error { case notEven(Int) }
let constraint = BlockConstraint<Int, Failure> { $0 % 2 == 0 } errorBuilder: { input in .notEven(input) } let result = constraint.evaluate(with: 2)
Declaration
Swift
public init(_ evaluationBlock: @escaping (T) -> Bool, errorBuilder: @escaping (T) -> E)
Parameters
evaluationBlock
A closure describing a custom validation condition.
errorBuilder
A generic closure that dynamically builds an
Error
to describe why the evaluation has failed. -
Evaluates the input against the provided evaluation closure.
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 theSummary
of the failingConstraint
s otherwise.