Spec-Zone.ru › Valibot

getDefaults

Returns the default values of the schema.

The difference to getDefault is that for object and tuple schemas this function recursively returns the default values of the subschemas instead of undefined.

const values = v.getDefaults<TSchema>(schema);

Generics

  • TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>

Parameters

  • schema TSchema

Returns

  • values InferDefaults<TSchema>

Examples

The following examples show how getDefaults can be used.

Object defaults

Get the default values of an object schema.

const ObjectSchema = v.object({
  key: v.optional(v.string(), "I'm the default!"),
});

const defaultValues = v.getDefaults(ObjectSchema); // { key: "I'm the default!" }

Tuple defaults

Get the default values of a tuple schema.

const TupleSchema = v.tuple([v.nullable(v.number(), 100)]);
const defaultValues = v.getDefaults(TupleSchema); // [100]

Related

The following APIs can be combined with getDefaults.

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

assert, config, fallback, keyof, message, omit, partial, pick, pipe, required, unwrap

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

Spec-Zone.ru

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