Spec-Zone.ru › Valibot

checkAsync

Creates a check validation action.

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

Generics

  • TInput extends any
  • TMessage extends ErrorMessage<CheckIssue<TInput>> | undefined

Parameters

  • requirement (input: TInput) => MaybePromise<boolean>
  • message TMessage

Explanation

With checkAsync you can freely validate the input and return true if it is valid or false otherwise. If the input does not match your requirement, you can use message to customize the error message.

Returns

  • Action CheckActionAsync<TInput, TMessage>

Examples

The following examples show how checkAsync can be used.

Cart item schema

Schema to check a cart item object.

import { getProductItem } from '~/api';

const CartItemSchema = v.pipeAsync(
  v.object({
    itemId: v.pipe(v.string(), v.regex(/^[a-z0-9]{10}$/i)),
    quantity: v.pipe(v.number(), v.minValue(1)),
  }),
  v.checkAsync(async (input) => {
    const productItem = await getProductItem(input.itemId);
    return productItem?.quantity >= input.quantity;
  }, 'The required quantity is greater than available.')
);

Related

The following APIs can be combined with checkAsync.

Schemas

any, array, 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

Utils

isOfKind, isOfType

Async

arrayAsync, awaitAsync, customAsync, exactOptionalAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, pipeAsync, recordAsync, setAsync, strictObjectAsync, strictTupleAsync, tupleAsync, tupleWithRestAsync, unionAsync, variantAsync

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

Spec-Zone.ru

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