rawCheckAsync
Creates a raw check validation action.
const Action = v.rawCheckAsync<TInput>(action);
Generics
TInputextends any
Parameters
action(context: Context<TInput>) => MaybePromise<void>
Explanation
With rawCheckAsync you can freely validate the input with a custom action and add issues if necessary.
Returns
ActionRawCheckActionAsync<TInput>
Examples
The following examples show how rawCheckAsync can be used.
Add users schema
Object schema that ensures that only users not already in the group are included.
This
rawCheckAsyncvalidation action adds an issue for any invalid username and forwards it viapathto the appropriate nested field.
import { isAlreadyInGroup } from '~/api';
const AddUsersSchema = v.pipeAsync(
v.object({
groupId: v.pipe(v.string(), v.uuid()),
usernames: v.array(v.pipe(v.string(), v.nonEmpty())),
}),
v.rawCheckAsync(async ({ dataset, addIssue }) => {
if (dataset.typed) {
await Promise.all(
dataset.value.usernames.map(async (username, index) => {
if (await isAlreadyInGroup(username, dataset.value.groupId)) {
addIssue({
received: username,
message: 'The user is already in the group.',
path: [
{
type: 'object',
origin: 'value',
input: dataset.value,
key: 'usernames',
value: dataset.value.usernames,
},
{
type: 'array',
origin: 'value',
input: dataset.value.usernames,
key: index,
value: username,
},
],
});
}
})
);
}
})
);
Related
The following APIs can be combined with rawCheckAsync.
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, forwardAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, pipeAsync, recordAsync, setAsync, strictObjectAsync, strictTupleAsync, tupleAsync, tupleWithRestAsync, undefinedableAsync, unionAsync, variantAsync
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/rawCheckAsync/