Code Beautifier
Unrecognized key(s) in object

Fix Zod "Unrecognized key(s) in object"

z.object().strict() rejects any key the schema does not declare. Why strict mode exists, and when to use .passthrough() or the default .strip() instead.

Input that triggers it

{"id": 1, "name": "Ada", "nickname": "countess"}
Open JSON to Zod on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

json
Zod schematypescript

What the error means

A Zod object schema has three ways to handle keys it was not told about. The default, .strip(), silently removes them from the parsed result. .passthrough() keeps them. .strict() rejects the input, with the issue Unrecognized key(s) in object: 'nickname' listing every unexpected key. The error therefore only appears when strictness was chosen — by hand, or by the "Strict objects" option in the generator above.

In the example, nickname is present in the input and absent from a schema generated from a sample without it, and the schema is strict.

Why it happens

  • The schema was generated from an incomplete sample, so keys that were absent from the sample are unknown to it.
  • The producer added a field, and the consumer's schema has not been updated.
  • Strict mode is on for a good reason: the schema guards a configuration object or an API request body, where an unexpected key is a typo or an attempt to set something that should not be settable.
  • A naming mismatch, nickName versus nickname, so a key that is declared looks unknown.

How to fix it

  1. Decide whether strictness is right here. For a request body or a config file, it usually is — reject the unknown key and fix the client. For a third-party API response, it usually is not — the producer is allowed to add fields.
  2. To keep strict mode, add the key. Paste a payload that includes every key into the generator above; each becomes a schema property, optional where it was missing from some samples.
  3. To relax it, remove .strict(). The default strips unknown keys from the output, which is safe for most consumers. Use .passthrough() only if downstream code needs the extra keys preserved.
  4. For a typo, fix the producer rather than accepting both spellings.

The schema that accepts the example under strict mode:

import { z } from "zod";

export const RootSchema = z
  .object({
    id: z.number().int(),
    name: z.string(),
    nickname: z.string().optional(),
  })
  .strict();

If it still fails

  • JSON Diff between the generation sample and the failing input lists the added keys by path.
  • Zod 4 renamed the behaviours: z.strictObject() and z.looseObject() replace .strict() and .passthrough() as top-level constructors; the semantics are unchanged.
  • The equivalent in JSON Schema is additionalProperties: false; JSON to JSON Schema can emit it for the same sample, and the error there reads must NOT have additional properties.

Related errors