picklist

Creates a picklist schema.

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

Generics

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

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.'
);

The following APIs can be combined with picklist.

Schemas

Methods

Actions

Utils

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of romansp
  • GitHub profile picture of sqmasep

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of antfu
  • GitHub profile picture of Thanaen
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of Unique-Pixels
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of nickytonline
  • GitHub profile picture of KubaJastrz
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of dslatkin