OptionalValidating
@dynamicMemberLookup
@propertyWrapper
public struct OptionalValidating<Value>
A type that wraps an optional of type Value
and an associated validator and re-validates every time the value is updated.
This type is the functional equivalent of Validating
but for optional values. This version should be used when you cannot provide a default value for a property on a type but still need to validate it, including if the value is required or not. Whilst it is preferable to use the Swift type system to declare whether or not a value is optional, it might not always be possible, e.g. if you need to capture some required value from user input but do not have any meaningful default that you can assign to the variable where it is declared.
Unless explicitly stated otherwise during initalisation, values are always treated as required, i.e. if the value is nil
then validation will return an .invalid
result. It is possible to specify that a value is not required in which case validation will only be run if the value is non-nil and otherwise will be treated as .valid
.
-
The optional wrapped value - changing the value will trigger validation.
Declaration
Swift
public var wrappedValue: Value? { get set }
-
Returns the
validatedValue
when using$value
syntax.Declaration
Swift
public var projectedValue: Validated<Value?, String> { get }
-
Initialises a new instance.
This initialiser will be called whenever you use the
@OptionalValidating
property wrapper and specify a default value for the optional property.By default, the value will be treated as required, which means any
nil
value will be treated as invalid.This method will automatically validate the initial
wrappedValue
, meaning avalidatedValue
will be available immediately after initialisation.You should pass in a non-optional validator - it will be converted to an optional validator automatically.
Declaration
Swift
public init(wrappedValue: Value?, required: Bool = true, _ validator: ValidatorOf<Value, String>)
Parameters
wrappedValue
The value being wrapped
required
If
true
,nil
values will be treated as invalid. Defaults totrue
.validator
The validator to use every time
wrappedValue
is changed. -
A convenience initialiser that takes a variadic list of validators and automatically combines them into a single validator.
Declaration
Swift
public init(wrappedValue: Value?, required: Bool = true, _ validators: ValidatorOf<Value, String>...)
-
A convenience initialiser that doesn’t require an initial
wrappedValue
.This method will be called when using the
@OptionalValidating
property wrapper with a property that has no initial value.You must explicitly state whether or not the value is required or not.
Declaration
Swift
public init(required: Bool, _ validator: ValidatorOf<Value, String>)
Parameters
required
If
true
,nil
values will be treated as invalid. Defaults totrue
.validator
The validator to use every time
wrappedValue
is changed. -
A convenience initialiser that doesn’t require an initial
wrappedValue
.This method takes a variadic list of validators and combines them into one automatically.
Declaration
Swift
public init(required: Bool, _ validators: ValidatorOf<Value, String>...)
-
Provides dynamic access to the underlying validated value’s properties.
Declaration
Swift
public subscript<T>(dynamicMember keyPath: KeyPath<Validated<Value?, String>, T>) -> T { get }