Structures

The following structures are available globally.

  • A type-erased Constraint.

    enum Failure: Error {
        case notEven
    }
    
    let constraint = BlockConstraint<Int, Failure> {
        $0 % 2 == 0
    } errorBuilder: {
        .notEven
    }
    
    let anyConstraint = AnyConstraint(constraint)
    anyConstraint.evaluate(with: 3)
    
    See more

    Declaration

    Swift

    public struct AnyConstraint<T, E> : Constraint where E : Error
  • Undocumented

    See more

    Declaration

    Swift

    @resultBuilder
    public struct ConstraintBuilder<T, E> 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)
    
    See more

    Declaration

    Swift

    public struct BlockConstraint<T, E> : Constraint where E : Error
  • Undocumented

    See more

    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
    }
    
    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)
    
    See more

    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 underlying Constraint.

    enum Failure: Error {
        case required
        case invalidEmail
    }
    
    let email: String? = "hello@nsagora.com"
    let constraint = OptionalConstraint<String, Failure>(required: .required) {
        PredicateConstraint(EmailPredicate(), error: .invalidEmail)
    }
    
    let result = constraint.evaluate(with: email)
    
    See more

    Declaration

    Swift

    public struct OptionalConstraint<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(EmailPredicate(), error: EmailFailure.invalid)
    let result = constraint.evaluate(with: "hello@nsagora.com)
    
    See more

    Declaration

    Swift

    public struct PredicateConstraint<T, E> : Constraint where 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
    }
    
    var constraint = TypeConstraint<RegistrationData, RegistrationData.Error> {
        KeyPathConstraint(\.username) {
            BlockConstraint {
                $0.count >= 5
            } errorBuilder: {
                .username
            }
        }
        KeyPathConstraint(\.password) {
            GroupConstraint(.all) {
                PredicateConstraint {
                    CharacterSetPredicate(.lowercaseLetters, mode: .inclusive)
                } errorBuilder: {
                    .password(.missingLowercase)
                }
                PredicateConstraint{
                    CharacterSetPredicate(.uppercaseLetters, mode: .inclusive)
                } errorBuilder: {
                    .password(.missingUppercase)
                }
                PredicateConstraint {
                    CharacterSetPredicate(.decimalDigits, mode: .inclusive)
                } errorBuilder: {
                    .password(.missingDigits)
                }
                PredicateConstraint {
                    CharacterSetPredicate(CharacterSet(charactersIn: "!?@#$%^&*()|\\/<>,.~`_+-="), mode: .inclusive)
                } errorBuilder: {
                    .password(.missingSpecialChars)
                }
                PredicateConstraint {
                    LengthPredicate(min: 8)
                }  errorBuilder: {
                    .password(.tooShort)
                }
            }
        }
        KeyPathConstraint(\.email) {
            PredicateConstraint(EmailPredicate(), error: .email)
        }
        KeyPathConstraint(\.age) {
            PredicateConstraint(RangePredicate(min: 14), error: .underAge)
        }
    }
    
    let user = RegistrationData(username: "nsagora", password: "p@ssW0rd", email: "hello@nsagora.com", age: 21)
    constraint.evaluate(with: user)
    
    See more

    Declaration

    Swift

    public struct TypeConstraint<T, E> : Constraint where E : Error
  • The summary of a constraint evaluation result.

    See more

    Declaration

    Swift

    public struct Summary<E> : Error where E : Error
    extension Summary: Equatable
  • A type-erased Predicate.

    let odd = BlockPredicate<Int> {
       $0 % 2 != 0
    }
    
    let anyOdd = AnyPredicate<Int>(odd)
    let isOdd = anyOdd.evaluate(with: 3)
    
    See more

    Declaration

    Swift

    public struct AnyPredicate<T> : Predicate
  • The BlockPredicate struct defines a closure based condition used to evaluate generic inputs.

    let predicate = BlockPredicate<Int> {
       $0 % 2 == 0
    }
    
    let isEven = even.evaluate(with: 2)
    
    See more

    Declaration

    Swift

    public struct BlockPredicate<T> : Predicate
  • The CharacterSetPredicate evaluates a String input agains a defined CharacterSet.

    let predicate = CharacterSetPredicate(.lowercaseLetters, mode: .inclusive)
    let containsLowercaseLetters = predicate.evaluate(with: "Letters")
    
    See more

    Declaration

    Swift

    public struct CharacterSetPredicate : 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.

    let predicate = EmailPredicate()
    let isEmail = predicate.evaluate(with: "hello@nsagora.com")
    
    See more

    Declaration

    Swift

    public struct EmailPredicate : Predicate
  • The LengthPredicate struct is used to evaluate whether a given input is of a given length.

    let predicate = LengthPredicate<String>(8...64)
    let isValid = predicate.evaluate(with: "p@ssW0rd")
    
    See more

    Declaration

    Swift

    public struct LengthPredicate<T> : Predicate where T : Collection
  • The RangePredicate struct is used to evaluate whether a given input is inside a given range.

    let drinkingAgeLimit = RangePredicate(min: 21)
    let isAllowed = drinkingAgeLimit.evaluate(with: 18)
    
    See more

    Declaration

    Swift

    public struct RangePredicate<T> : Predicate where T : Comparable
  • The RegexPredicate struct is used to define regular expression based conditions used to evaluate input strings.

    let predicate = RegexPredicate(expression: "^\\d+$")
    let isValid = predicate.evaluate(with: "1234567890")
    
    See more

    Declaration

    Swift

    public struct RegexPredicate : Predicate
  • The RequiredPredicate struct is used to evaluate whether a given input is empty.

    let predicate = RequiredPredicate<String>()
    let isValid = predicate.evaluate(with: "")
    
    See more

    Declaration

    Swift

    public struct RequiredPredicate<T> : Predicate where T : Collection
  • The URLPredicate struct is used to evaluate whether a given input is a syntactically valid URL.

    let predicate = URLPredicate()
    let isValid = predicate.evaluate(with: "http://www.swift.org")
    
    See more

    Declaration

    Swift

    public struct URLPredicate : Predicate