every
Strickland provides an every
validator. The every
validator operates over an array and it will only return a valid result if all validators in the array are valid. The every
validator will short-circuit (exit early) as soon as an invalid result is encountered. This allows chaining of validators where one validator's logic might depend on a previous validator already being valid.
Parameters
The first parameter to the every
validator factory is the array of validators. Validator props can also be supplied either as an object or as a function that accepts context and returns a validator props object.
Result Properties
every
: The array of validation results produced during validation
The every
validator adds an every
property to the validation result with the validation results of every validator that was validated in the array of validators. Validators that were not executed because of validation failing early will be omitted from this array. The validation result property is named every
to match the name of the validator (this is a common pattern in Strickland).
There are a few notable characteristics of this result:
The properties from each executed validator are added to the top-level result
The
required
validator added therequired: true
property to the resultThe
minLength
validator added theminLength: 5
property to the result
Property collisions are resolved using last-in-wins
In this example, the
message
from therequired
validator was replaced with themessage
from theminLength
validatorThis allows the message to reflect the deepest validation result in the array
Once validation fails for a validator, no further validation is performed
The
maxLength
validator was not executedThe props from the
maxLength
validator are not included on the resultThe
every
array in the result does not include an item for themaxLength
validator
The top-level
isValid
prop on the result reflects the overall validation result
Last updated