Spec-Zone.ru › Valibot

pipe

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

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

Generics

  • TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>
  • TItems extends readonly PipeItem<any, unknown, BaseIssue<unknown>>[]

Parameters

  • schema TSchema
  • items TItems

Explanation

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

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

The pipe 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 SchemaWithPipe<readonly [TSchema, ...TItems]>

Examples

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

Email schema

Schema to validate an email.

const EmailSchema = v.pipe(
  v.string(),
  v.nonEmpty('Please enter your email.'),
  v.email('The email is badly formatted.'),
  v.maxLength(30, 'Your email is too long.')
);

String to number

Schema to convert a string to a number.

const NumberSchema = v.pipe(v.string(), v.transform(Number), v.number());

Related

The following APIs can be combined with pipe.

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, 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, toBigint, toBoolean, toCamelCase, toDate, toKebabCase, toLowerCase, toMaxValue, toMinValue, toNumber, toPascalCase, toSnakeCase, toString, toUpperCase, transform, trim, trimEnd, trimStart, ulid, url, uuid, value, values, words

Utils

isOfKind, isOfType

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

Spec-Zone.ru

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