Apply constraints
Constraints are assertions that a condition is true. Applied to a Validatable
, they allow describing a set of rules that must be followed by the validated value; otherwise the validation will fail.
Basic usage
Constraints are extension functions of the Validatable
class. Some can be applied to all wrapped types, some others only to specific ones. For example, equalTo
can be applied to all wrapped types, whereas hasLengthLowerThan
can only be applied to Validatable<CharSequence>
.
To apply a constraint, call it on a property inside a Validator
:
Some constraints also accept parameters:
Read constraint violations
Once validation is done, it returns a ValidationResult
, which is a sealed interface composed of two classes:
ValidationResult.Success
: the validation has succeeded, it contains the validated value.ValidationResult.Failure
: the validation has failed, it contains a violation list. Each violation contains the path of the property, and the message describing why it failed.
Here's a usage example:
The output is the following:
Group violations by path
To group the violations by their path, use byPath
:
The output is the following:
Throw on failed validation
In some cases, validation failure should be treated as an exception. Instead of manually creating and throwing an exception, one could use orThrow
:
Customization
Each constraint returns a Constraint
object, which allows you to customize the message and the path.
Custom message
Use otherwise
to provide a custom message for a constraint:
Remember the value can be unwrapped, allowing to use it within custom messages:
Custom path
Use withPath
combined to absolute
to provide a custom path for a constraint:
The constraint violation path will be [a, b, c]
.
Other methods are available to cover your needs:
- absolute
Generates a new path absolute path.
Code:absolute("a", "b", "c")
Path:[a, b, c]
- relative
Replaces the property name by the provided components.
Code:relative("a", "b", "c")
Path:[author, a, b, c]
- appended
Appends the provided components to the current path.
Code:appended("a", "b", "c")
Path:[author, fullName, a, b, c]
What those methods essentially do is returning a List<String>
. When in need of greater customization, you can return the path you want based on the current path, which is passed as a parameter:
The path will be [author, a, b, c, fullName]
.
Fail on first violation
In some performance-critical environments, you might need your validator to fail as fast as possible. This can be enabled with the failOnFirstViolation
configuration option.
Let's reuse the example of Read constraint violations and modify its configuration:
The output now contains only one constraint violation, the first one: