Spec-Zone.ru › Valibot

checkItems

Creates a check items validation action.

const Action = v.checkItems<TInput, TMessage>(requirement, message);

Generics

  • TInput extends ArrayInput
  • TMessage extends ErrorMessage<CheckItemsIssue<TInput>> | undefined

Parameters

  • requirement ArrayRequirement<TInput>
  • message TMessage

Explanation

With checkItems you can freely validate the items of an array and return true if they are valid or false otherwise. If an item does not match your requirement, you can use message to customize the error message.

The special thing about checkItems is that it automatically forwards each issue to the appropriate item.

Returns

  • Action CheckItemsAction<TInput, TMessage>

Examples

The following examples show how checkItems can be used.

No duplicate items

Schema to validate that an array has no duplicate items.

const ArraySchema = v.pipe(
  v.array(v.string()),
  v.checkItems(
    (item, index, array) => array.indexOf(item) === index,
    'Duplicate items are not allowed.'
  )
);

Related

The following APIs can be combined with checkItems.

Schemas

any, array, custom, instance, tuple, unknown

Methods

pipe

Utils

isOfKind, isOfType

© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/checkItems/

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API