Defjs
Core

struct.object

Struct that parses a plain object with a fixed set of fields.

@defjs/core · Function

Signature

function object<T extends ObjectShape>(
  shape: T,
): ObjectStruct<T>;

shape maps field names to structs.

  • Every field is required by default. A missing key fails with missing_key.

  • .optional() and .nullish() allow a missing key. An absent optional field stays absent in the output.

  • .null() / .nullable() allow null.

  • .alias(name) sets the field's wire key. struct.parse reads logical keys by default; { aliases: true } and HTTP JSON decoding read wire keys, and HTTP JSON encoding writes them. Parsed output uses logical keys.

  • Keys that are not in shape are removed from the output. The output is a new object with a null prototype.

  • The input must be a plain object. Arrays, Map, and class instances fail with invalid_type (expected object).

  • Parsing stops at the first failing field, and issue.path holds its key.

  • Throws TypeError when shape is not a plain object.

ObjectShape is { [key: string]: any }; each value must be a struct.

Parameters

Prop

Type

shape T
Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao