Structures
The following structures are available globally.
-
An error that occurs while validating a value.
See moreDeclaration
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 theFailable
initializer:try Failable<String, Length1028>("the quick brown fox...")
Or you can call
.failable
on any intance of a type conforming toCustomStringConvertible
:try "the quick brown fox...".failable(Length1028.self)
Mutation
A
Failable
type is a struct, so the stored value can only be mutated if theFailable
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 thevalue
typeT
also supports it. Initialization is supported forInt
,Float
,Bool
,nil
, andString
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.
See moreDictionary
andArray
types are not supported for literal initialization yet because array splatting for variadic parameters is not supported yet.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
See moreSupported
type.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
See morevalidate
method calls.forEach
on the value passed in and passes theValidator.run
method in as the closure.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
See morenil
,ValidationError.valueIsNil
is thrown.Declaration
Swift
public struct NotNil<Wrapped>: Validation
-
Validates a value if it is not
See morenil
. If the value isnil
, the validation is skipped.Declaration
Swift
public struct NotNilValidate<V>: Validation where V: Validation