maxLength
The length of a string or array is at most max.
Signature
ts
function maxLength(max: number, msg?: Message): Action<{ length: number }>Parameters
| Parameter | Type | Description |
|---|---|---|
max | number | Maximum length (inclusive) |
msg | Message | Custom error message. A string with {params} or a function |
Error codes
| Code | When | Default message |
|---|---|---|
maxLength | A string is longer than max | Must be at most {max} {max|character|characters} long |
maxItems | An array has more than max items | Must have at most {max} {max|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
Fits a database column
ts
pipe(string(), trim(), maxLength(255))Up to 5 files
ts
pipe(array(string()), maxLength(5))