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. Extensibility

Validation Context

PreviousValidator FactoriesNextValidation Result Props

Last updated 4 years ago

Was this helpful?

Validators sometimes need additional context at the time of validation. We've seen our letter validator can accept a letter prop through a , but what if the comparison value isn't known until the time of validation? To support this, Strickland's validate function accepts a context argument after the value to be validated, and the context is passed to the validator. Validators can then utilize context to determine validator prop values.

Let's extend our letter validator to support the validator props being supplied as a function. That function will be called at the time of validation, supplying the context. This is added while retaining the ability to accept the letter prop directly in the factory.

import validate from 'strickland';

function letterValidator(validatorProps) {
    return function validateLetter(value, context) {
        // Be sure not to overwrite the original
        // validatorProps variable
        let resolvedProps = validatorProps;

        if (typeof resolvedProps === 'function') {
            resolvedProps = resolvedProps(context);
        }

        resolvedProps = resolvedProps || {};

        const {letter} = resolvedProps;
        return (value === letter);
    }
}

const validator = letterValidator((context) => ({letter: context.letter}));
const result = validate(validator, 'B', {letter: 'B'});

/*
    result = {
        isValid: true,
        value: 'B'
    }
 */

Strickland's validate function always passes a validation context object to the validator so that validators do not need to defend against undefined or null validation context. The value being validated will always be provided on the validation context as a value context prop.

validator factory