record

Creates a record schema.

// Record schema with an optional pipe
const Schema = record<TValue>(value, pipe);

// Record schema with an optional message and pipe
const Schema = record<TValue>(value, message, pipe);

// Record schema with key and optional pipe
const Schema = record<TKey, TValue>(key, value, pipe);

// Record schema with key and optional message and pipe
const Schema = record<TKey, TValue>(key, value, message, pipe);

Generics

Parameters

Explanation

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

If key is not specified, the keys are validated with string.

Returns

Examples

The following examples show how record can be used.

String record schema

Schema to validate a record with strings.

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

Object record schema

Schema to validate a record of objects.

const ObjectRecordSchema = record(object({ key: string() }));

Picklist as key

Schema to validate a record with specific keys.

const ProductRecordSchema = record(
  picklist(['product_a', 'product_b', 'product_c']),
  number()
);

Enum as key

Schema to validate a record with specific keys.

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

const ProductRecordSchema = record(enum_(Products), number());

The following APIs can be combined with record.

Schemas

Methods

Transformations

Validations

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of dailydotdev
  • GitHub profile picture of ivan-mihalic
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper