KeyPathConstraint

public struct KeyPathConstraint<T, V, E> : Constraint where E : Error

A Constraint that evaluates a property on a piece of data by it’s key path.

struct LoginData {

    enum Error: Swift.Error {
        case email
        case password
    }

    var email: String
    var password: String
}
let constraint = KeyPathConstraint<LoginData, String, LoginData.Error>(\.email) {
    PredicateConstraint(EmailPredicate(), error: .email)
}

let data = LoginData(email: "hello@nsagora.com", password: "p@ssW0rd")
constraint.evaluate(with: data)
  • Declaration

    Swift

    public typealias InputType = T
  • Declaration

    Swift

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

    struct LoginData {
    
        enum Error: Swift.Error {
            case email
            case password
        }
    
        var email: String
        var password: String
    }
    
    let constraint = KeyPathConstraint<LoginData, String, LoginData.Error>(
        \.email,
        constraint: PredicateConstraint(EmailPredicate(), error: .email)
    )
    
    let data = LoginData(email: "hello@nsagora.com", password: "p@ssW0rd")
    constraint.evaluate(with: data)
    

    Declaration

    Swift

    public init<C>(_ keyPath: KeyPath<T, V>, constraint: C) where V == C.InputType, E == C.ErrorType, C : Constraint

    Parameters

    keyPath

    A

    constraint

    A Constraint to describes the evaluation rule for the property at the provided key path.

  • Returns a new

    struct LoginData {
    
        enum Error: Swift.Error {
            case email
            case password
        }
    
        var email: String
        var password: String
    }
    
    let constraint = KeyPathConstraint<LoginData, String, LoginData.Error>(\.email) {
        PredicateConstraint(EmailPredicate(), error: .email)
    }
    
    let data = LoginData(email: "hello@nsagora.com", password: "p@ssW0rd")
    constraint.evaluate(with: data)
    

    Declaration

    Swift

    public init<C>(_ keyPath: KeyPath<T, V>, constraintBuilder: () -> C) where V == C.InputType, E == C.ErrorType, C : Constraint
  • Evaluates the value of the property at the key path against the underlying constraints.

    Declaration

    Swift

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

    Parameters

    input

    The input to be validated.

    Return Value

    .success if the value of the input’s property is valid,.failure containing the Summary of the failing Constraints otherwise.