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(.email, 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(.email, error: .email) ) let data = LoginData(email: "hello@nsagora.com", password: "p@ssW0rd") constraint.evaluate(with: data)
Declaration
Swift
public init(_ keyPath: KeyPath<T, V>, constraint: some Constraint<V, E>)
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(.email, error: .email) } let data = LoginData(email: "hello@nsagora.com", password: "p@ssW0rd") constraint.evaluate(with: data)
Declaration
Swift
public init(_ keyPath: KeyPath<T, V>, constraintBuilder: () -> some Constraint<V, E>)
-
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 theSummary
of the failingConstraint
s otherwise.