checkAsync
Creates a check validation action.
const Action = v.checkAsync<TInput, TMessage>(requirement, message);
Generics
TInputextends anyTMessageextends ErrorMessage<CheckIssue<TInput>> | undefined
Parameters
requirement(input: TInput) => MaybePromise<boolean>messageTMessage
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
ActionCheckActionAsync<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
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/