array
Создает схему массива.
const Schema = v.array<TItem, TMessage>(item, message);
Дженерики
TItemextends BaseSchema<unknown, unknown, BaseIssue<unknown>>TMessageextends ErrorMessage<ArrayIssue> | undefined
Параметры
itemTItemmessageTMessage
Объяснение
С помощью array вы можете валидировать тип входных данных. Если входные данные не являются массивом, вы можете использовать message для настройки сообщения об ошибке.
Если ваш массив имеет фиксированную длину, рассмотрите возможность использования
tupleдля более точной типизации.
Возвращаемое значение
SchemaArraySchema<TItem, TMessage>
Примеры
Следующие примеры показывают, как можно использовать array.
Схема массива строк
Схема для валидации массива строк.
const StringArraySchema = v.array(v.string(), 'An array is required.');
Схема массива объектов
Схема для валидации массива объектов.
const ObjectArraySchema = v.array(v.object({ key: v.string() }));
Валидация длины
Схема, проверяющая длину массива.
const ArrayLengthSchema = v.pipe(
v.array(v.number()),
v.minLength(1),
v.maxLength(3)
);
Валидация содержимого
Схема, проверяющая содержимое массива.
const ArrayContentSchema = v.pipe(
v.array(v.string()),
v.includes('foo'),
v.excludes('bar')
);
Связанные ресурсы
Следующие API могут быть скомбинированы с array.
Схемы
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
Методы
assert, config, fallback, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser
Действия
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
Утилиты
entriesFromList, isOfKind, isOfType
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/array/