array
Creates an array schema.
const Schema = v.array<TItem, TMessage>(item, message);
Generics
TItemextends BaseSchema<unknown, unknown, BaseIssue<unknown>>TMessageextends ErrorMessage<ArrayIssue> | undefined
Parameters
itemTItemmessageTMessage
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
tuplefor a more precise typing.
Returns
SchemaArraySchema<TItem, TMessage>
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')
);
Related
The following APIs can be combined with array.
Schemas
any, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, optional, picklist, promise, record, set, strictObject, strictTuple, string, symbol, tuple, tupleWithRest, undefined, undefinedable, union, unknown, variant, void
Methods
assert, config, fallback, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser
Actions
check, checkItems, brand, description, empty, everyItem, excludes, filterItems, findItem, flavor, guard, includes, length, mapItems, maxLength, metadata, minLength, nonEmpty, notLength, partialCheck, rawCheck, rawTransform, readonly, reduceItems, someItem, sortItems, title, transform
Utils
entriesFromList, isOfKind, isOfType
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/array/