array

Creates an array schema.

const Schema = v.array<TItem, TMessage>(item, message);

Generics

Parameters

  • item TItem
  • message TMessage

Explanation

With array you can validate the data type of the input. 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 = v.array(v.string(), 'An array is required.');

Object array schema

Schema to validate an array of objects.

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

Validate length

Schema that validates the length of an array.

const ArrayLengthSchema = v.pipe(
  v.array(v.number()),
  v.minLength(1),
  v.maxLength(3)
);

Validate content

Schema that validates the content of an array.

const ArrayContentSchema = v.pipe(
  v.array(v.string()),
  v.includes('foo'),
  v.excludes('bar')
);

The following APIs can be combined with array.

Schemas

Methods

Actions

Utils

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 antfu
  • GitHub profile picture of Thanaen
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of fabulousgk
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of isoden
  • GitHub profile picture of nickytonline
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of luckasnix
  • GitHub profile picture of andrew-3kb
  • GitHub profile picture of dslatkin