minLength
The length of a string or array is at least min.
Signature
ts
function minLength(min: number, msg?: Message): Action<{ length: number }>Parameters
| Parameter | Type | Description |
|---|---|---|
min | number | Minimum length (inclusive) |
msg | Message | Custom error message. A string with {params} or a function |
Error codes
| Code | When | Default message |
|---|---|---|
minLength | A string is shorter than min | Must be at least {min} {min|character|characters} long |
minItems | An array has fewer than min items | Must have at least {min} {min|item|items} |
You can change the texts: see Messages & i18n.
Notes
- Works with strings and arrays.
- String length is counted in UTF-16 units: emoji and some rare characters take 2 units (
'😀'.length === 2).
Examples
Password
ts
pipe(string(), minLength(8, 'At least {min} characters'))At least one item
ts
pipe(array(string()), minLength(1, 'Select at least one option'))