Skip to content

minLength ​

The length of a string or array is at least min.

Signature ​

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

Parameters ​

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

Error codes ​

CodeWhenDefault message
minLengthA string is shorter than minMust be at least {min} {min|character|characters} long
minItemsAn array has fewer than min itemsMust 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'))

See also ​