Strickland
Search…
maxLength
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 required validator.

Named Props

  • maxLength: The maximum length compared against

Parameters

The maxLength validator supports three parameter signatures:
  1. 1.
    maxLength(value) where the value is used as the maxLength named prop
  2. 2.
    maxLength(propsObject) where the props object contains a maxLength named prop
  3. 3.
    maxLength(propsFunction) where the props function returns a props object with a maxLength named prop

Usage

1
import validate, {maxLength} from 'strickland';
2
3
// As a value parameter
4
const maxLengthOf3 = maxLength(3);
5
6
// As a named prop
7
const maxLengthOf2 = maxLength({
8
maxLength: 2,
9
message: 'Must have a length of at most 2'
10
});
11
12
// As a function that resolves to have the named prop
13
const maxLengthValidator = maxLength((context) => ({
14
maxLength: context.maxLength,
15
message: `Must have a length of at most ${context.maxLength}`
16
}));
Copied!
Last modified 1yr ago