picklist
Creates a picklist schema.
const Schema = v.picklist<TOptions, TMessage>(options, message);
Generics
TOptionsextends PicklistOptionsTMessageextends ErrorMessage<PicklistIssue> | undefined
Parameters
optionsTOptionsmessageTMessage
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.
picklistworks in a similar way toenum. However, in many cases it is easier to use because you can pass an array of values instead of an enum.
Returns
SchemaPicklistSchema<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/