Skip to content

maxLength ​

The length of a string or array is at most max.

Signature ​

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

Parameters ​

ParameterTypeDescription
maxnumberMaximum length (inclusive)
msgMessageCustom error message. A string with {params} or a function

Error codes ​

CodeWhenDefault message
maxLengthA string is longer than maxMust be at most {max} {max|character|characters} long
maxItemsAn array has more than max itemsMust 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))

See also ​