Structures

The following structures are available globally.

  • An error that occurs while validating a value.

    See more

    Declaration

    Swift

    public struct ValidationError: Codable, Error
  • A type that can fail when being set because the new value does pass certain validations.

    You can create a Failable instance in 2 ways. The first is to use the Failable initializer:

    try Failable<String, Length1028>("the quick brown fox...")
    

    Or you can call .failable on any intance of a type conforming to CustomStringConvertible:

    try "the quick brown fox...".failable(Length1028.self)
    

    Mutation

    A Failable type is a struct, so the stored value can only be mutated if the Failable instance is a variable. The stored value does not make its setter public, because then you would be able to set the value directly and bypass the validations. Instead you use the <~ operator to assign a new value:

    var story = try "the quick brown fox...".failable(Length1028.self)
    try story <~ "Long long ago, on a pencil far far away..."
    

    Use the .value property to access the actual value:

    var story = try "the quick brown fox...".failable(Length1028.self)
    print(story.value)
    

    Literal Initialization

    Failable supprts initialization with certain type literals if the value type T also supports it. Initialization is supported for Int, Float, Bool, nil, and String types.

    let string = Failable<String, EmptyValidationM<String>> = "Hello world"
    

    Warning

    Because literal initializers cannot fail, your program will crash if the value passed in does not pass validation.

    Dictionary and Array types are not supported for literal initialization yet because array splatting for variadic parameters is not supported yet.

    See more

    Declaration

    Swift

    public struct Failable<T, Validations> where Validations: Validation, Validations.Supported == T
  • A combination of two validations.

    This validation is used for things such as numeric operations where Failable types with different validations are used.

    The validations used must have the same Supported type.

    See more

    Declaration

    Swift

    public struct AppendedValidations<T, V1, V2>: Validation where V1: Validation, V2: Validation, V1.Supported == T, V2.Supported == T
  • Validates each element in a sequence using another validator.

    The validate method calls .forEach on the value passed in and passes the Validator.run method in as the closure.

    See more

    Declaration

    Swift

    public struct ElementValidation<Seq, Validator>: Validation where Seq: Sequence, Validator: Validation, Validator.Supported == Seq.Element
  • Verified that a value is not equal to nil.

    If the value passed in to be validated is nil, ValidationError.valueIsNil is thrown.

    See more

    Declaration

    Swift

    public struct NotNil<Wrapped>: Validation
  • Validates a value if it is not nil. If the value is nil, the validation is skipped.

    See more

    Declaration

    Swift

    public struct NotNilValidate<V>: Validation where V: Validation