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

length

PreviousmaxLengthNextComposition

Last updated 4 years ago

Was this helpful?

The length validator combines the minLength and maxLength validators to check that the length of a string value is within a range. Both the minLength and maxLength values are inclusive.

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

Named Props

  • minLength: The minimum value compared against

  • maxLength: The maximum value compared against

Parameters

The length validator supports three parameter signatures:

  1. length(minLength, maxLength) where the minLength and maxLength named props are specified as values

  2. length(propsObject) where the props object contains minLength and maxLength named props

  3. length(propsFunction) where the props function returns a props object with minLength and maxLength named props

Usage

import validate, {length} from 'strickland';

// As value parameters
const maxLengthBetween5and10 = length(5, 10);

// As named props
const maxLengthBetween10and20 = length({
    minLength: 10,
    maxLength: 20,
    message: 'Must have a length between 10 and 20'
});

// As a function that resolves to have the named props
const lengthValidator = length((context) => ({
    minLength: context.minLength,
    maxLength: context.maxLength,
    message: `Must have a length between ${context.minLength} and ${context.maxLength}`
}));
required