Strickland
  • Readme
  • Introduction
    • Validators
    • Validation
    • Validation Results
  • Extensibility
    • Validator Factories
    • Validation Context
    • Validation Result Props
    • Extensibility Pattern
    • formatResult
  • Built-In Validators
    • required
    • compare
    • min
    • max
    • range
    • minLength
    • maxLength
    • length
  • Composition
    • Arrays of Validators
      • every
      • all
      • some
    • Validating Array Elements
      • arrayElements
    • Validating Objects
      • objectProps
      • Advanced Object Validation
      • Nested Objects
      • Arrays of Objects
    • Composition Conventions
    • Composition and formatResult
  • Async Validation
    • Resolving Async Validation
    • Deferred Async Validation
    • Async Validator Arrays and Objects
    • Two-Stage Sync/Async Validation
    • Race Conditions
    • Automatic Race Condition Handling
    • Async Validation and formatResult
  • Form Validation
    • form
    • Async Form Validation
    • validateFields
    • emptyResults
    • updateFieldResults
  • Inspiration
  • Design Goals
  • Wrap-Up
  • Change Log
  • NPM
  • GitHub
Powered by GitBook
On this page

Was this helpful?

  1. Composition

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.

import validate from './strickland';

export default function every(validators) {
    return function validateEvery(value, context) {
        let result = {
            value,
            isValid: true
        };

        validators.every((validator) => {
            let validatorResult = validate(
                validator, value, context
            );

            result = {
                ...result,
                ...validatorResult,
                isValid: validatorResult.isValid
            };

            return result.isValid;
        });

        return result;
    }
}

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.

import validate, {every, required, minLength} from 'strickland';

const mustExistWithLength5 = every([
    required(),
    minLength(5)
]);

const result = validate(mustExistWithLength5, '1234', {
    message: 'Must have a length of at least 5'
});

/*
    result = {
        isValid: false,
        value: '1234',
        required: true,
        minLength: 5,
        length: 4
    }
 */

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

PreviousCompositionNextevery

Last updated 4 years ago

Was this helpful?

every
all
some