Spec-Zone.ru › Valibot

pick

Creates a modified copy of an object schema that contains only the selected entries.

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

Generics

  • TSchema extends SchemaWithoutPipe<LooseObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined> | LooseObjectSchemaAsync<ObjectEntriesAsync, ErrorMessage<ObjectIssue> | undefined> | ObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined> | ObjectSchemaAsync<ObjectEntriesAsync, ErrorMessage<ObjectIssue> | undefined> | ObjectWithRestSchema<ObjectEntries, BaseSchema<unknown, unknown, BaseIssue<unknown>>, ErrorMessage<ObjectWithRestIssue> | undefined> | ObjectWithRestSchemaAsync<ObjectEntriesAsync, BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>, ErrorMessage<ObjectWithRestIssue> | undefined> | StrictObjectSchema<ObjectEntries, ErrorMessage<ObjectIssue> | undefined> | StrictObjectSchemaAsync<ObjectEntriesAsync, ErrorMessage<ObjectIssue> | undefined>>
  • TKeys extends ObjectKeys<TSchema>

Parameters

  • schema TSchema
  • keys TKey

Explanation

pick creates a modified copy of the given object schema that contains only the selected keys. It is similar to TypeScript's Pick utility type.

Because pick 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 pick.

Returns

  • Schema SchemaWithPick<TSchema, TKeys>

Examples

The following examples show how pick can be used.

Pick specific keys

Schema that contains only the selected keys of an existing schema.

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

Related

The following APIs can be combined with pick.

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, partial, required, safeParse, safeParser, unwrap

Actions

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

Utils

isOfKind, isOfType

Async

arrayAsync, checkAsync, exactOptionalAsync, fallbackAsync, getDefaultsAsync, getFallbacksAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, parseAsync, parserAsync, partialAsync, partialCheckAsync, rawCheckAsync, rawTransformAsync, recordAsync, requiredAsync, safeParseAsync, safeParserAsync, setAsync, strictObjectAsync, strictTupleAsync, transformAsync, tupleAsync, tupleWithRestAsync, undefinedableAsync, unionAsync

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

Spec-Zone.ru

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