Spec-Zone.ru › Valibot

custom

Создаёт пользовательскую схему.

Эта функция схемы позволяет определить схему, которая проверяет значение с помощью пользовательской функции. Используйте её, когда нужно определить схему, которую нельзя выразить с помощью других функций схем.

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

Обобщённые типы

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

Параметры

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

Пояснение

С помощью custom можно проверить тип данных входного значения. Если входное значение не соответствует проверке check, можно использовать message, чтобы настроить сообщение об ошибке.

Убедитесь, что проверка в check соответствует типу данных TInput.

Возвращаемое значение

  • Schema CustomSchema<TInput, TMessage>

Примеры

В следующих примерах показано, как можно использовать custom.

Схема строки пикселей

Схема для проверки строки пикселей.

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

Связанные материалы

Следующие API можно использовать вместе с custom.

Схемы

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

Методы

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

Действия

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

Утилиты

entriesFromList, isOfKind, isOfType

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

Spec-Zone.ru

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