flavor
Creates a flavor transformation action.
const Action = v.flavor<TInput, TName>(name);
Generics
TInputextends anyTNameextends FlavorName
Parameters
nameTName
Explanation
flavor is a less strict version of brand that allows you to flavor the output type of a schema with a name. Data is considered valid if it's type is unflavored or has been validated by a schema that has the same flavor.
flavorcan also be used as a TypeScript DX hack to improve the editor's autocompletion by displaying only literal types, but still allowing the unflavored root type to be passed.
Returns
ActionFlavorAction<TInput, TName>
Examples
The following examples show how flavor can be used.
Flavored ID schemas
Schema to ensure that different types of IDs are not mixed up.
// Create user ID and order ID schema
const UserIdSchema = v.pipe(v.string(), v.flavor('UserId'));
const OrderIdSchema = v.pipe(v.string(), v.flavor('OrderId'));
// Infer output types of both schemas
type UserId = v.InferOutput<typeof UserIdSchema>;
type OrderId = v.InferOutput<typeof OrderIdSchema>;
// This works because output is flavored
const userId: UserId = v.parse(UserIdSchema, 'c28443ef...');
const orderId: OrderId = v.parse(OrderIdSchema, '4b717520...');
// You can also use unflavored strings
const newUserId1: UserId = '2d80cd94...';
// But this will result in a type error
const newUserId2: UserId = orderId;
Related
The following APIs can be combined with flavor.
Schemas
any, array, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, optional, picklist, promise, record, set, strictObject, strictTuple, string, symbol, tuple, tupleWithRest, undefined, undefinedable, union, unknown, variant, void
Methods
Utils
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/flavor/