Spec-Zone.ru › Valibot

partial

Creates a modified copy of an object schema that marks all or only the selected entries as optional.

const Schema = v.partial<TSchema, TKeys>(schema, keys);

Generics

  • TSchema extends SchemaWithoutPipe<LooseObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined> | ObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined> | ObjectWithRestSchema<ObjectEntries, BaseSchema<unknown, unknown, BaseIssue<unknown>>, ErrorMessage<ObjectWithRestIssue> | undefined> | StrictObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined>>
  • TKeys extends ObjectKeys<TSchema> | undefined

Parameters

  • schema TSchema
  • keys TKey

Explanation

partial creates a modified copy of the given object schema where all entries or only the selected keys are optional. It is similar to TypeScript's Partial utility type.

Because partial changes the data type of the input and output, it is not allowed to pass a schema that has been modified by the pipe method, as this may cause runtime errors. Please use the pipe method after you have modified the schema with partial.

Returns

  • Schema SchemaWithPartial<TSchema, TKeys>

Examples

The following examples show how partial can be used.

Partial object schema

Schema to validate an object with partial entries.

const PartialSchema = v.partial(
  v.object({
    key1: v.string(),
    key2: v.number(),
  })
); // { key1?: string; key2?: number }

With only specific keys

Schema to validate an object with only specific entries marked as optional.

const PartialSchema = v.partial(
  v.object({
    key1: v.string(),
    key2: v.number(),
    key3: v.boolean(),
  }),
  ['key1', 'key3']
); // { key1?: string; key2: number; key3?: boolean }

Related

The following APIs can be combined with partial.

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, forward, getDefault, getDefaults, getFallback, getFallbacks, is, keyof, message, omit, parse, parser, pick, required, safeParse, safeParser, unwrap

Actions

check, brand, description, entries, flavor, guard, maxEntries, metadata, minEntries, notEntries, partialCheck, rawCheck, rawTransform, readonly, title, transform

Utils

isOfKind, isOfType

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

Spec-Zone.ru

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