Spec-Zone.ru › Valibot

custom

Creates a custom schema.

This schema function allows you to define a schema that matches a value based on a custom function. Use it whenever you need to define a schema that cannot be expressed using any of the other schema functions.

const Schema = v.custom<TInput, TMessage>(check, message);

Generics

  • TInput extends any
  • TMessage extends ErrorMessage<CustomIssue> | undefined = ErrorMessage<CustomIssue> | undefined

Parameters

  • check (input: unknown) => boolean
  • message TMessage

Explanation

With custom you can validate the data type of the input. If the input does not match the validation of check, you can use message to customize the error message.

Make sure that the validation in check matches the data type of TInput.

Returns

  • Schema CustomSchema<TInput, TMessage>

Examples

The following examples show how custom can be used.

Pixel string schema

Schema to validate a pixel string.

const PixelStringSchema = v.custom<`${number}px`>((input) =>
  typeof input === 'string' ? /^\d+px$/.test(input) : false
);

Related

The following APIs can be combined with custom.

Schemas

array, exactOptional, intersect, lazy, looseObject, looseTuple, map, nonNullable, nonNullish, nonOptional, nullable, nullish, object, objectWithRest, optional, record, set, strictObject, strictTuple, tuple, tupleWithRest, undefinedable, union

Methods

assert, config, fallback, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser

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

entriesFromList, isOfKind, isOfType

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

Spec-Zone.ru

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