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
  • Named Props
  • Parameters
  • Usage

Was this helpful?

  1. Built-In Validators

compare

PreviousrequiredNextmin

Last updated 4 years ago

Was this helpful?

The compare validator is quite similar to the letter validator we've built in our examples.

If the value being validated is null, false, an empty string, or another falsy value other than 0, then the result will be valid. This respects the rule of thumb described in the notes for the validator.

Named Props

  • compare: The value compared against

Parameters

The compare validator supports three parameter signatures:

  1. compare(value) where the value is used as the compare named prop

  2. compare(propsObject) where the props object contains a compare named prop

  3. compare(propsFunction) where the props function returns a props object with a compare named prop

Usage

import validate, {compare} from 'strickland';

// As a value parameter
const letterA = compare('A');

// As a named prop
const letterB = compare({
    compare: 'B',
    message: 'Must be the letter "B"'
});

// Using a function that resolves to have the named prop
const letterValidator = compare((context) => ({
    compare: context.compare,
    message: `Must match "${context.compare}"`
}));
required