all
Strickland provides an all validator. The all validator operates over an array and it will only return a valid result if all validators in the array are valid. But all has a significant difference from every: all will always execute all validators, regardless of previous results. You can use all if all validators are safe to execute and you need to know all validator results, even if some are invalid.
Parameters
The first parameter to the all 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.
const atLeast5Chars = all(
[
required(),
minLength(5)
],
{message: 'Must have at least 5 characters'}
);
const result = validate(atLeast5Chars, '1234');
const requiredWithMinLength = all(
[
required(),
minLength((context) => ({minLength: context.minLength}))
],
(context) => ({message: `Must have at least ${context.minLength} characters`})
);Result Properties
all: The array of validation results produced during validation
The all validator adds an all property to the validation result with the validation results of all validators that were validated in the array of validators. The validation result property is named all to match the name of the validator (this is a common pattern in Strickland).
Usage
import validate, {
all, required, minLength, maxLength
} from 'strickland';
const mustExistWithLength5to10 = all([
required({message: 'Required'}),
minLength({minLength: 5, message: 'Must have at least 5 characters'}),
maxLength({maxLength: 10, message: 'Must have at most 10 characters'})
]);
const result = validate(mustExistWithLength5to10, '1234');
/*
result = {
isValid: false,
value: '1234',
required: true,
minLength: 5,
message: 'Must have at most 10 characters',
all: [
{
isValid: true,
value: '1234',
required: true,
message: 'Required'
},
{
isValid: false,
value: '1234',
minLength: 5,
message: 'Must have at least 5 characters'
},
{
isValid: true,
value: '1234',
maxLength: 10,
message: 'Must have at most 10 characters'
}
]
}
*/There are a few notable characteristics of this result:
The properties from all executed validators are added to the top-level result
The
requiredvalidator added therequired: trueproperty to the resultThe
minLengthvalidator added theminLength: 5property to the resultThe
maxLengthvalidator added themaxLength: 10property to the result
Property collisions are resolved using last-in-wins
In this example, the
messagefrom themaxLengthvalidator replaced the messages provided by therequiredandminLengthvalidatorsThis behavior is consistent and predictable with Strickland, but limits how top-level result properties can be used with the
allvalidator
All validators are executed, even after the result is known to be invalid
The top-level
isValidprop on the result reflects the overall validation result
Last updated
Was this helpful?