Skip to content

length ​

The length of a string or array is exactly length.

Signature ​

ts
function length(length: number, msg?: Message): Action<{ length: number }>

Parameters ​

ParameterTypeDescription
lengthnumberExact length
msgMessageCustom error message. A string with {params} or a function

Error codes ​

CodeWhenDefault message
lengthString length differsMust be exactly {length} {length|character|characters} long
itemsNumber of items differsMust 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+$/))

See also ​