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

range

PreviousmaxNextminLength

Last updated 4 years ago

Was this helpful?

The range validator combines the min and max validators to check that a value is within a range. Both the min and max values are inclusive.

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

  • min: The minimum value compared against

  • max: The maximum value compared against

Parameters

The range validator supports three parameter signatures:

  1. range(min, max) where the min and max named props are specified as values

  2. range(propsObject) where the props object contains min and max named props

  3. range(propsFunction) where the props function returns a props object with min and max named props

Usage

import validate, {range} from 'strickland';

// As value parameters
const between5and10 = range(5, 10);

// As named props
const between10and20 = range({
    min: 10,
    max: 20,
    message: 'Must be between 10 and 20'
});

// As a function that resolves to have the named props
const rangeValidator = range((context) => ({
    min: context.min,
    max: context.max,
    message: `Must be between ${context.min} and ${context.max}`
}));
required