# record

> This document is the Markdown version of [valibot.dev/api/record/](https://valibot.dev/api/record/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Creates a record schema.

```ts
const Schema = v.record<TKey, TValue, TMessage>(key, value, message);
```

## Generics

- `TKey` `extends BaseSchema<string, string | number | symbol, BaseIssue<unknown>>`
- `TValue` `extends BaseSchema<unknown, unknown, BaseIssue<unknown>>`
- `TMessage` `extends ErrorMessage<RecordIssue> | undefined`

## Parameters

- `key` `TKey`
- `value` `TValue`
- `message` `TMessage`

### Explanation

With `record` you can validate the data type of the input and whether the entries matches `key` and `value`. If the input is not an object, you can use `message` to customize the error message.

> This schema filters certain entries from the record for security reasons.

> This schema marks an entry as optional if it detects that its key is a literal type. The reason for this is that it is not technically possible to detect missing literal keys without restricting the `key` schema to [`string`](/api/string.md), [`enum`](/api/enum.md) and [`picklist`](/api/picklist.md). However, if [`enum`](/api/enum.md) and [`picklist`](/api/picklist.md) are used, it is better to use [`object`](/api/object.md) with [`entriesFromList`](/api/entriesFromList.md) because it already covers the needed functionality. This decision also reduces the bundle size of `record`, because it only needs to check the entries of the input and not any missing keys.

## Returns

- `Schema` `RecordSchema<TKey, TValue, TMessage>`

## Examples

The following examples show how `record` can be used.

### String record schema

Schema to validate a record with strings.

```ts
const StringRecordSchema = v.record(
  v.string(),
  v.string(),
  'An object is required.'
);
```

### Object record schema

Schema to validate a record of objects.

```ts
const ObjectRecordSchema = v.record(v.string(), v.object({ key: v.string() }));
```

### Picklist as key

Schema to validate a record with specific optional keys.

```ts
const ProductRecordSchema = v.record(
  v.picklist(['product_a', 'product_b', 'product_c']),
  v.optional(v.number())
);
```

### Enum as key

Schema to validate a record with specific optional keys.

```ts
enum Products {
  PRODUCT_A = 'product_a',
  PRODUCT_B = 'product_b',
  PRODUCT_C = 'product_c',
}

const ProductRecordSchema = v.record(v.enum(Products), v.optional(v.number()));
```

## Related

The following APIs can be combined with `record`.

### Schemas

[`any`](/api/any.md), [`array`](/api/array.md), [`bigint`](/api/bigint.md), [`blob`](/api/blob.md), [`boolean`](/api/boolean.md), [`custom`](/api/custom.md), [`date`](/api/date.md), [`enum`](/api/enum.md), [`exactOptional`](/api/exactOptional.md), [`file`](/api/file.md), [`function`](/api/function.md), [`instance`](/api/instance.md), [`intersect`](/api/intersect.md), [`lazy`](/api/lazy.md), [`literal`](/api/literal.md), [`looseObject`](/api/looseObject.md), [`looseTuple`](/api/looseTuple.md), [`map`](/api/map.md), [`nan`](/api/nan.md), [`never`](/api/never.md), [`nonNullable`](/api/nonNullable.md), [`nonNullish`](/api/nonNullish.md), [`nonOptional`](/api/nonOptional.md), [`null`](/api/null.md), [`nullable`](/api/nullable.md), [`nullish`](/api/nullish.md), [`number`](/api/number.md), [`object`](/api/object.md), [`objectWithRest`](/api/objectWithRest.md), [`optional`](/api/optional.md), [`picklist`](/api/picklist.md), [`promise`](/api/promise.md), [`set`](/api/set.md), [`strictObject`](/api/strictObject.md), [`strictTuple`](/api/strictTuple.md), [`string`](/api/string.md), [`symbol`](/api/symbol.md), [`tuple`](/api/tuple.md), [`tupleWithRest`](/api/tupleWithRest.md), [`undefined`](/api/undefined.md), [`undefinedable`](/api/undefinedable.md), [`union`](/api/union.md), [`unknown`](/api/unknown.md), [`variant`](/api/variant.md), [`void`](/api/void.md)

### Methods

[`assert`](/api/assert.md), [`config`](/api/config.md), [`fallback`](/api/fallback.md), [`getDefault`](/api/getDefault.md), [`getDefaults`](/api/getDefaults.md), [`getFallback`](/api/getFallback.md), [`getFallbacks`](/api/getFallbacks.md), [`is`](/api/is.md), [`message`](/api/message.md), [`parse`](/api/parse.md), [`parser`](/api/parser.md), [`pipe`](/api/pipe.md), [`safeParse`](/api/safeParse.md), [`safeParser`](/api/safeParser.md)

### Actions

[`check`](/api/check.md), [`brand`](/api/brand.md), [`description`](/api/description.md), [`entries`](/api/entries.md), [`flavor`](/api/flavor.md), [`guard`](/api/guard.md), [`maxEntries`](/api/maxEntries.md), [`minEntries`](/api/minEntries.md), [`metadata`](/api/metadata.md), [`partialCheck`](/api/partialCheck.md), [`rawCheck`](/api/rawCheck.md), [`rawTransform`](/api/rawTransform.md), [`readonly`](/api/readonly.md), [`title`](/api/title.md), [`transform`](/api/transform.md)

### Utils

[`entriesFromList`](/api/entriesFromList.md), [`isOfKind`](/api/isOfKind.md), [`isOfType`](/api/isOfType.md)
