minLength
Creates a min length validation action.
const Action = v.minLength<TInput, TRequirement, TMessage>(
requirement,
message
);
Generics
TInputextends LengthInputTRequirementextends numberTMessageextends ErrorMessage<MinLengthIssue<TInput, TRequirement>> | undefined
Parameters
requirementTRequirementmessageTMessage
Explanation
With minLength you can validate the length of a string or array. For strings, length is measured like JavaScript's value.length, i.e. by UTF-16 code units. For arrays, length is measured by the number of items. If the input does not match the requirement, you can use message to customize the error message.
If you need to validate the minimum number of user-perceived characters, use
minGraphemesinstead. If you need to count each Unicode code point as a single character (a supplementary code point occupies two UTF-16 code units in JavaScript), useminCodePointsinstead.
Returns
ActionMinLengthAction<TInput, TRequirement, TMessage>
Examples
The following examples show how minLength can be used.
Minimum string length
Schema to validate a string with a minimum length of 3 UTF-16 code units.
const MinStringSchema = v.pipe(
v.string(),
v.minLength(3, 'The string must be 3 or more UTF-16 code units long.')
);
Minimum array length
Schema to validate an array with a minimum length of 5 items.
const MinArraySchema = v.pipe(
v.array(v.number()),
v.minLength(5, 'The array must contain 5 numbers or more.')
);
Related
The following APIs can be combined with minLength.
Schemas
any, array, custom, instance, string, tuple, unknown
Methods
Actions
Utils
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/minLength/