length
The length of a string or array is exactly length.
Signature
ts
function length(length: number, msg?: Message): Action<{ length: number }>Parameters
| Parameter | Type | Description |
|---|---|---|
length | number | Exact length |
msg | Message | Custom error message. A string with {params} or a function |
Error codes
| Code | When | Default message |
|---|---|---|
length | String length differs | Must be exactly {length} {length|character|characters} long |
items | Number of items differs | Must have exactly {length} {length|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).
Example
SMS code
ts
pipe(string(), length(6), pattern(/^\d+$/))