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

maxLength

PreviousminLengthNextlength

Last updated 4 years ago

Was this helpful?

The maxLength validator checks that a string value has a length at most the maximum length provided.

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

  • maxLength: The maximum length compared against

Parameters

The maxLength validator supports three parameter signatures:

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

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

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

Usage

import validate, {maxLength} from 'strickland';

// As a value parameter
const maxLengthOf3 = maxLength(3);

// As a named prop
const maxLengthOf2 = maxLength({
    maxLength: 2,
    message: 'Must have a length of at most 2'
});

// As a function that resolves to have the named prop
const maxLengthValidator = maxLength((context) => ({
    maxLength: context.maxLength,
    message: `Must have a length of at most ${context.maxLength}`
}));
required