Structures
The following structures are available globally.
-
A type that acts as a generic wrapper around a validate function.
This type is the building block for all validations provided by the library.
When writing your own validators - either from scratch, or by composing existing ones - it is recommended that you encapsulate these by creating static variables (or functions, if your validator takes parameters) on extensions of
ValidatorOf
, constrainted to the type value type that the validator operates on.You should also constrain your extension on the particular type of error that you return - in most cases this will simply be a
String
.The following example shows you can define your own validator that operates on an
Int
:extension ValidatorOf where Value == String, Error == String { static func myIntValidator(someParameter: Int) -> Self { return Self { value in // your validation logic } } }
Defining your validators in this way allows you to pass an instance into anything that takes a
ValidatorOf<Int, String>
(such as the@Validating
property wrapper) in concise form by taking advantage of Swift type-inference, e.g.:
See more@Validating(.myIntValidator(someParameter: 1))
Declaration
Swift
public struct ValidatorOf<Value, Error>
-
A type that wraps a value of type
Value
and an associated validator and re-validates every time value is updated.This type can be used to associate a value and its validation rules and have that value be re-validated every time it changes, check if it is valid and if not, what the validation errors are. It has dynamic member lookup support to provide direct access to the validated value.
Its primary use is as a property wrapper that lets you declaratively validate properties on your types. When used this way, you can access the validated version of your property using the $property syntax.
See morestruct MyViewModel { @Validating(.hasLengthOf(.atLeast(6)) var password: String = "" } var viewModel = MyViewModel() viewModel.$password.isValid // returns false viewModel.password = "mypassword" viewModel.$password.isValid // returns true
Declaration
Swift
@dynamicMemberLookup @propertyWrapper public struct Validating<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
See morenil
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
.Declaration
Swift
@dynamicMemberLookup @propertyWrapper public struct OptionalValidating<Value>