Arrays of Validators
Values often need to be validated against multiple validators. This can be represented in Strickland by using arrays of validators with a top-level validator that operates over the array. The top-level validator can invoke the validate
function to collect results from each validator and combine the results into a top-level validation result.
Here is a validator that validates every validator in an array, short-circuiting as soon as an invalid result is encountered.
The every
validator uses the factory pattern, accepting an array of validators and returning a function to validate every one of the validators. Because validators can accept validation context, those must be accepted and passed through.
Here is how the every
validator can be used.
Built-In Composition Validators
Strickland has a few built-in composition validators that operate over arrays of validators.
Returns a valid result if all validators are valid
Short-circuits upon the first invalid result
Returns a valid result if all validators are valid
Does not short-circuit, producing results for all validators
Returns a valid result if any validator is valid
Short-circuits upon the first valid result
Last updated