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

Validator Factories

Validators often need to be configurable. Instead of always validating that a value matches the letter 'A', our letter validator might need to accept which letter to compare against. To accomplish that, we can define a Validator Factory that accepts props, including the desired letter, and returns a validator to be used.

import validate from 'strickland';

function letterValidator({letter}) {
    return (value) => value === letter;
}

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

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

Validator factories merely take advantage of JavaScript's functional nature--in fact, Strickland has no awareness of them. Strickland only requires that validators are functions that accept a value and return a validation result. You can produce those functions however you'd like, but the validator factory approach is a convenient way to extend the functionality of your validators.

PreviousExtensibilityNextValidation Context

Last updated 4 years ago

Was this helpful?