Spec-Zone.ru › Valibot

pipeAsync

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

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

Generics

  • TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>
  • TItems extends readonly (PipeItem<any, unknown, BaseIssue<unknown>> | PipeItemAsync<any, unknown, BaseIssue<unknown>>)[]

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

  • Schema SchemaWithPipeAsync<readonly [TSchema, ...TItems]>

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})`
  )
);

Related

The following APIs can be combined with pipeAsync.

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

Methods

assert, config, fallback, forward, getDefault, getDefaults, getFallback, getFallbacks, is, keyof, message, omit, parse, parser, partial, pick, required, safeParse, safeParser, unwrap

Actions

args, base64, bic, brand, bytes, check, checkItems, codePoints, creditCard, cuid2, decimal, description, digits, domain, email, emoji, empty, endsWith, entries, everyItem, excludes, filterItems, findItem, finite, flavor, graphemes, gtValue, guard, hash, hexadecimal, hexColor, imei, includes, integer, ip, ipv4, ipv6, isbn, isrc, isoDate, isoDateTime, isoDateTimeSecond, isoTime, isoTimeSecond, isoTimestamp, isoWeek, ksuid, length, ltValue, mac, mac48, mac64, mapItems, maxBytes, maxCodePoints, maxEntries, maxGraphemes, maxLength, maxSize, maxValue, maxWords, metadata, mimeType, minBytes, minCodePoints, minEntries, minGraphemes, minLength, minSize, minValue, minWords, multipleOf, nanoid, nonEmpty, notBytes, notCodePoints, notEntries, notGraphemes, notLength, notSize, notValue, notValues, notWords, octal, parseJson, partialCheck, rawCheck, rawTransform, readonly, reduceItems, regex, returns, rfcEmail, safeInteger, size, slug, someItem, sortItems, startsWith, stringifyJson, title, toCamelCase, toKebabCase, toLowerCase, toMaxValue, toMinValue, toPascalCase, toSnakeCase, toUpperCase, transform, trim, trimEnd, trimStart, ulid, url, uuid, value, values, words

Utils

isOfKind, isOfType

Async

arrayAsync, awaitAsync, checkAsync, customAsync, exactOptionalAsync, fallbackAsync, forwardAsync, getDefaultsAsync, getFallbacksAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, parseAsync, parserAsync, partialAsync, partialCheckAsync, rawCheckAsync, rawTransformAsync, recordAsync, requiredAsync, safeParseAsync, safeParserAsync, setAsync, strictObjectAsync, strictTupleAsync, transformAsync, tupleAsync, tupleWithRestAsync, undefinedableAsync, unionAsync, variantAsync

© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/pipeAsync/

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API