lazyAsync

Creates a lazy schema.

const Schema = v.lazyAsync<TWrapped>(getter);

Generics

Parameters

Explanation

The getter function is called lazily to retrieve the schema. This is necessary to be able to access the input through the first argument of the getter function and to avoid a circular dependency for recursive schemas.

Returns

Examples

The following examples show how lazyAsync can be used.

Transaction list schema

Recursive schema to validate transactions.

Due to a TypeScript limitation, the input and output types of recursive schemas cannot be inferred automatically. Therefore, you must explicitly specify these types using GenericSchemaAsync.

import { isTransactionValid } from '~/api';

type Transaction = {
  transactionId: string;
  next: Transaction | null;
};

const TransactionSchema: v.GenericSchemaAsync<Transaction> = v.objectAsync({
  transactionId: v.pipeAsync(
    v.string(),
    v.uuid(),
    v.checkAsync(isTransactionValid, 'The transaction is not valid.')
  ),
  next: v.nullableAsync(v.lazyAsync(() => TransactionSchema)),
});

Email or username schema

Schema to validate an object containing an email or username.

In most cases, unionAsync and variantAsync are the better choices for creating such a schema. I recommend using lazyAsync only in special cases.

import { isEmailPresent, isUsernamePresent } from '~/api';

const EmailOrUsernameSchema = v.lazyAsync((input) => {
  if (input && typeof input === 'object' && 'type' in input) {
    switch (input.type) {
      case 'email':
        return v.objectAsync({
          type: v.literal('email'),
          email: v.pipeAsync(
            v.string(),
            v.email(),
            v.checkAsync(
              isEmailPresent,
              'The email is not present in the database.'
            )
          ),
        });
      case 'username':
        return v.objectAsync({
          type: v.literal('username'),
          username: v.pipeAsync(
            v.string(),
            v.nonEmpty(),
            v.checkAsync(
              isUsernamePresent,
              'The username is not present in the database.'
            )
          ),
        });
    }
  }
  return v.never();
});

The following APIs can be combined with lazyAsync.

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 Unique-Pixels
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of nickytonline
  • GitHub profile picture of KubaJastrz
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of dslatkin