object

Creates an object schema.

// Object schema with an optional pipe
const Schema = object<TEntries>(entries, pipe);

// Object schema with an optional message and pipe
const Schema = object<TEntries>(entries, message, pipe);

// Object schema with rest and optional pipe
const Schema = object<TEntries, TRest>(entries, rest, pipe);

// Object schema with rest and optional message and pipe
const Schema = object<TEntries, TRest>(entries, rest, message, pipe);

Generics

Parameters

Explanation

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

By default, object removes unknown entries from the input. You can change and control this behavior with rest. Read more here.

Returns

Examples

The following examples show how object can be used. Please see the object guide for more examples and explanations.

Simple object schema

Schema to validate an object with two keys.

const SimpleObjectSchema = object({
  key1: string(),
  key2: number(),
});

Object schema with rest

Schema to validate an object with generic rest entries.

const ObjectSchemaWithRest = object(
  {
    key1: string(),
    key2: number(),
  },
  boolean()
);

Strict object schema

Schema to validate a strict object that prevent unknown entries.

const StrictObjectSchema = object(
  {
    key1: string(),
    key2: number(),
  },
  never()
);

Merge objects

Schema that merges the entries of two object schemas.

const MergedObjectSchema = merge([
  object({ key1: string() }),
  object({ key2: number() }),
]);

Partial object schema

Schema to validate an object with partial entries.

const PartialObjectSchema = partial(
  object({
    key1: string(),
    key2: number(),
  })
);

The following APIs can be combined with object.

Schemas

Methods

Transformations

Validations

Contributors

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

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of kazizi55

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