pipeAsync

Adds a pipeline to a schema, that can validate and transform its input.

const Schema = v.pipeAsync<TSchema, TItems>(schema, ...items);

Generics

Parameters

  • schema TSchema
  • items TItems

Explanation

pipeAsync creates a modified copy of the given schema, containing a pipeline for detailed validations and transformations. It passes the input data asynchronously through the items in the order they are provided and each item can examine and modify it.

Since pipeAsync returns a schema that can be used as the first argument of another pipeline, it is possible to nest multiple pipeAsync calls to extend the validation and transformation further.

pipeAsync aborts early and marks the output as untyped if issues were collected before attempting to execute a schema or transformation action as the next item in the pipeline, to prevent unexpected behavior.

Returns

Examples

The following examples show how pipeAsync can be used. Please see the pipeline guide for more examples and explanations.

Stored email schema

Schema to validate a stored email address.

import { isEmailPresent } from '~/api';

const StoredEmailSchema = v.pipeAsync(
  v.string(),
  v.nonEmpty('Please enter your email.'),
  v.email('The email is badly formatted.'),
  v.maxLength(30, 'Your email is too long.'),
  v.checkAsync(isEmailPresent, 'The email is not in the database.')
);

New user schema

Schema to validate and transform new user details to a string.

import { isUsernameUnique } from '~/api';

const NewUserSchema = v.pipeAsync(
  v.objectAsync({
    firstName: v.pipe(v.string(), v.nonEmpty(), v.maxLength(30)),
    lastName: v.pipe(v.string(), v.nonEmpty(), v.maxLength(30)),
    username: v.pipeAsync(
      v.string(),
      v.nonEmpty(),
      v.maxLength(30),
      v.checkAsync(isUsernameUnique, 'The username is not unique.')
    ),
  }),
  v.transform(
    ({ firstName, lastName, username }) =>
      `${username} (${firstName} ${lastName})`
  )
);

The following APIs can be combined with pipeAsync.

Schemas

Methods

Actions

Utils

Async

Contributors

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

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

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 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 caegdeveloper
  • GitHub profile picture of andrew-d-jackson
  • GitHub profile picture of dslatkin