Structures
The following structures are available globally.
-
A type-erased
Constraint
.
See moreenum Failure: Error { case notEven } let constraint = BlockConstraint<Int, Failure> { $0 % 2 == 0 } errorBuilder: { .notEven } let anyConstraint = AnyConstraint(constraint) anyConstraint.evaluate(with: 3)
Declaration
Swift
public struct AnyConstraint<T, E> : Constraint where E : Error
-
Declaration
Swift
@resultBuilder public struct ConstraintBuilder<T, E> where E : Error
-
A
Constraint
that links a custom validation closure to anError
that describes why the evaluation has failed.enum Failure: Error { case notEven }
See morelet constraint = BlockConstraint<Int, Failure> { $0 % 2 == 0 } errorBuilder: { .notEven } let result = constraint.evaluate(with: 2)
Declaration
Swift
public struct BlockConstraint<T, E> : Constraint where E : Error
-
Declaration
Swift
public struct GroupConstraint<T, 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 }
See morelet 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 struct KeyPathConstraint<T, V, E> : Constraint where E : Error
-
A
Constraint
that accepts an optional input and passes the unwrapped value to an underlyingConstraint
.enum Failure: Error { case required case invalidEmail }
See morelet email: String? = "hello@nsagora.com" let constraint = OptionalConstraint<String, Failure>(required: .required) { PredicateConstraint(.email, error: .invalidEmail) } let result = constraint.evaluate(with: email)
Declaration
Swift
public struct OptionalConstraint<T, E> : Constraint where E : Error
-
A
Constraint
that links aPredicate
to anError
that describes why the predicate evaluation has failed.
See morelet constraint = PredicateConstraint(.email, error: EmailFailure.invalid) let result = constraint.evaluate(with: "hello@nsagora.com)
Declaration
Swift
public struct PredicateConstraint<T, E> : Constraint where E : Error
-
A
Constraint
that check whether the input collection is empty.enum Failure: Error { case required case invalidEmail }
See morelet constraint = RequiredConstraint<String, Failure>(error: .required) let result = constraint.evaluate(with: "hello@nsagora.com")
Declaration
Swift
public struct RequiredConstraint<T, E> : Constraint where T : Collection, E : Error
-
A
Constraint
that allows to evaluate complex data types.struct RegistrationData { enum Error: Swift.Error { case username case password(Password) case email case underAge } enum Password { case missingUppercase case missingLowercase case missingDigits case missingSpecialChars case tooShort } var username: String var password: String var email: String var age: Int }
See morevar constraint = TypeConstraint<RegistrationData, RegistrationData.Error> { KeyPathConstraint(\.username) { BlockConstraint { $0.count >= 5 } errorBuilder: { .username } } KeyPathConstraint(\.password) { GroupConstraint(.all) { PredicateConstraint { .characterSet(.lowercaseLetters, mode: .inclusive) } errorBuilder: { .password(.missingLowercase) } PredicateConstraint{ .characterSet(.uppercaseLetters, mode: .inclusive) } errorBuilder: { .password(.missingUppercase) } PredicateConstraint { .characterSet(.decimalDigits, mode: .inclusive) } errorBuilder: { .password(.missingDigits) } PredicateConstraint { .characterSet(CharacterSet(charactersIn: "!?@#$%^&*()|\\/<>,.~`_+-="), mode: .inclusive) } errorBuilder: { .password(.missingSpecialChars) } PredicateConstraint { .length(min: 8) } errorBuilder: { .password(.tooShort) } } } KeyPathConstraint(\.email) { PredicateConstraint(.email, error: .email) } KeyPathConstraint(\.age) { PredicateConstraint(.range(min: 14), error: .underAge) } } let user = RegistrationData(username: "nsagora", password: "p@ssW0rd", email: "hello@nsagora.com", age: 21) constraint.evaluate(with: user)
Declaration
Swift
public struct TypeConstraint<T, E> : Constraint where E : Error
-
The summary of a constraint evaluation result.
See moreDeclaration
Swift
public struct Summary<E> : Error where E : Error
extension Summary: Equatable
-
The
DatePredicate
struct is used to evaluate that a given input has a valid date format.
See morelet dateFormatter = DateFormatter() dateFormatter.dateFormat = "YYYY-MM-dd" let predicate = DatePredicate(formatter: dateFormatter) let isValidDate = predicate.evaluate(with: "2021-12-06")
Declaration
Swift
public struct DatePredicate : Predicate
-
The
EmailPredicate
struct is used to evaluate whether a given input is a syntactically valid email address, based on the RFC 5322 official standard.
See morelet predicate = EmailPredicate() let isEmail = predicate.evaluate(with: "hello@nsagora.com")
Declaration
Swift
public struct EmailPredicate : Predicate