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,
nickNameversusnickname, so a key that is declared looks unknown.
How to fix it
- 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.
- 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.
- 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. - 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()andz.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 readsmust NOT have additional properties.
Related errors
Expected number, received string
Use z.coerce.number() when strings are expected (query params, form fields), or fix the producer to send a number; z.string() if the value is really an identifier.
json: unknown field
Add the missing field to the struct (regenerate from a fuller sample), or stop calling DisallowUnknownFields if extra keys are acceptable; the default decoder ignores them.