array

Creates an array schema.

// Array schema with an optional pipe
const Schema = array<TItem>(item, pipe);

// Array schema with an optional message and pipe
const Schema = array<TItem>(item, message, pipe);

Generics

Parameters

Explanation

With array you can validate the data type of the input and with pipe you can transform and validate the further details of the array. If the input is not an array, you can use message to customize the error message.

If your array has a fixed length, consider using tuple for a more precise typing.

Returns

Examples

The following examples show how array can be used.

String array schema

Schema to validate an array of strings.

const StringArraySchema = array(string(), 'An array is required.');

Object array schema

Schema to validate an array of objects.

const ObjectArraySchema = array(object({ key: string() }));

Validate length

Schema that validates the length of an array.

const ArrayLengthSchema = array(number(), [minLength(1), maxLength(3)]);

Validate content

Schema that validates the content of an array.

const ArrayContentSchema = array(string(), [includes('foo'), excludes('bar')]);

The following APIs can be combined with array.

Schemas

Methods

Transformations

Validations

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of kazizi55

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of dailydotdev
  • GitHub profile picture of ivan-mihalic
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper