Spec-Zone.ru › Valibot

brand

Creates a brand transformation action.

const Action = v.brand<TInput, TName>(name);

Generics

  • TInput extends any
  • TName extends BrandName

Parameters

  • name TName

Explanation

brand allows you to brand the output type of a schema with a name. This ensures that data can only be considered valid if it has been validated by a particular branded schema.

Returns

  • Action BrandAction<TInput, TName>

Examples

The following examples show how brand can be used.

Branded fruit schema

Schema to ensure that only a validated fruit is accepted.

// Create schema and infer output type
const FruitSchema = v.pipe(v.object({ name: v.string() }), v.brand('Fruit'));
type FruitOutput = v.InferOutput<typeof FruitSchema>;

// This works because output is branded
const apple: FruitOutput = v.parse(FruitSchema, { name: 'apple' });

// But this will result in a type error
const banana: FruitOutput = { name: 'banana' };

Related

The following APIs can be combined with brand.

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

pipe

Utils

isOfKind, isOfType

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

Spec-Zone.ru

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