Spec-Zone.ru › Valibot

picklist

Creates a picklist schema.

const Schema = v.picklist<TOptions, TMessage>(options, message);

Generics

  • TOptions extends PicklistOptions
  • TMessage extends ErrorMessage<PicklistIssue> | undefined

Parameters

  • options TOptions
  • message TMessage

Explanation

With picklist you can validate that the input corresponds to a picklist option. If the input is invalid, you can use message to customize the error message.

picklist works in a similar way to enum. However, in many cases it is easier to use because you can pass an array of values instead of an enum.

Returns

  • Schema PicklistSchema<TOptions, TMessage>

Examples

The following examples show how picklist can be used.

Language schema

Schema to validate programming languages.

const LanguageSchema = v.picklist(['JavaScript', 'TypeScript']);

Country schema

Schema to validate country codes.

const countries = [
  { name: 'Germany', code: 'DE' },
  { name: 'France', code: 'FR' },
  { name: 'United States', code: 'US' },
] as const;

const CountrySchema = v.picklist(
  countries.map((country) => country.code),
  'Please select your country.'
);

Related

The following APIs can be combined with picklist.

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

check, brand, description, flavor, guard, metadata, rawCheck, rawTransform, readonly, title, transform

Utils

entriesFromList, isOfKind, isOfType

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

Spec-Zone.ru

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