customAsync

Creates a custom schema.

This schema function allows you to define a schema that matches a value based on a custom function. Use it whenever you need to define a schema that cannot be expressed using any of the other schema functions.

const Schema = v.customAsync<TInput, TMessage>(check, message);

Generics

Parameters

  • check (input: unknown) => MaybePromise<boolean>
  • message TMessage

Explanation

With customAsync you can validate the data type of the input. If the input does not match the validation of check, you can use message to customize the error message.

Make sure that the validation in check matches the data type of TInput.

Returns

Examples

The following examples show how customAsync can be used.

Vacant seat schema

Schema to validate a vacant seat.

import { isSeatVacant } from '~/api';

type Group = 'A' | 'B' | 'C' | 'D' | 'E';
type DigitLessThanSix = '0' | '1' | '2' | '3' | '4' | '5';
type Digit = DigitLessThanSix | '6' | '7' | '8' | '9';
type Seat = `${Group}${DigitLessThanSix}${Digit}`;

function isSeat(possibleSeat: string): possibleSeat is Seat {
  return /^[A-E][0-5]\d$/.test(possibleSeat);
}

const VacantSeatSchema = v.customAsync<Seat>(
  (input) => typeof input === 'string' && isSeat(input) && isSeatVacant(input),
  'The input is not a valid vacant seat.'
);

The following APIs can be combined with customAsync.

Schemas

Methods

Actions

Utils

Async

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of EltonLobo07

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of antfu
  • GitHub profile picture of Thanaen
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of fabulousgk
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of isoden
  • GitHub profile picture of nickytonline
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of luckasnix
  • GitHub profile picture of andrew-3kb
  • GitHub profile picture of dslatkin