Arrays

To validate arrays with a schema you can use array or tuple. You use tuple if your array has a specific shape and array if it has any number of uniform items.

Array schema

The first argument you pass to array is a schema, which is used to validate the items of the array.

import * as v from 'valibot';

const ArraySchema = v.array(v.number()); // number[]

Pipeline validation

To validate the length or contents of the array, you can use a pipeline.

import * as v from 'valibot';

const ArraySchema = v.pipe(
  v.array(v.string()),
  v.minLength(1),
  v.maxLength(5),
  v.includes('foo'),
  v.excludes('bar')
);

Tuple schema

A tuple is an array with a specific shape. The first argument that you pass to the function is a tuple of schemas that defines its shape.

import * as v from 'valibot';

const TupleSchema = v.tuple([v.string(), v.number()]); // [string, number]

Loose and strict tuples

The tuple schema removes unknown items. This means that items that you have not defined in the first argument are not validated and added to the output. You can change this behavior by using the looseTuple or strictTuple schema instead.

The looseTuple schema allows unknown items and adds them to the output. The strictTuple schema forbids unknown items and returns an issue for the first unknown item found.

Tuple with specific rest

Alternatively, you can also use the tupleWithRest schema to define a specific schema for unknown items. Any items not defined in the first argument are then validated against the schema of the second argument.

import * as v from 'valibot';

const TupleSchema = v.tupleWithRest([v.string(), v.number()], v.null());

Pipeline validation

Similar to arrays, you can use a pipeline to validate the length and contents of a tuple.

import * as v from 'valibot';

const TupleSchema = v.pipe(
  v.tupleWithRest([v.string()], v.string()),
  v.maxLength(5),
  v.includes('foo'),
  v.excludes('bar')
);

Contributors

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

  • GitHub profile picture of fabian-hiller

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 Thanaen
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of seahindeniz