requiredAsync

Creates a modified copy of an object schema that marks all or only the selected entries as required.

const AllKeysSchema = v.requiredAsync<TSchema, TMessage>(schema, message);
const SelectedKeysSchema = v.requiredAsync<TSchema, TKeys, TMessage>(
  schema,
  keys,
  message
);

Generics

Parameters

  • schema TSchema
  • keys TKey
  • message TMessage

Explanation

requiredAsync creates a modified copy of the given object schema where all or only the selected keys are required. It is similar to TypeScript's Required utility type.

Because requiredAsync changes the data type of the input and output, it is not allowed to pass a schema that has been modified by the pipeAsync method, as this may cause runtime errors. Please use the pipeAsync method after you have modified the schema with requiredAsync.

Returns

Examples

The following examples show how requiredAsync can be used.

New task schema

Schema to validate an object containing task details.

import { isOwnerPresent } from '~/api';

const UpdateTaskSchema = v.objectAsync({
  owner: v.optionalAsync(
    v.pipeAsync(
      v.string(),
      v.email(),
      v.checkAsync(isOwnerPresent, 'The owner is not in the database.')
    )
  ),
  title: v.optional(v.pipe(v.string(), v.nonEmpty(), v.maxLength(255))),
  description: v.optional(v.pipe(v.string(), v.nonEmpty())),
});

const NewTaskSchema = v.requiredAsync(UpdateTaskSchema);

/*
  {
    owner: string;
    title: string;
    description: string;
  }
*/

The following APIs can be combined with requiredAsync.

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