skip to content

As the lead of a TypeScript service on Zod 4, would you standardise request and response schemas on strip, strict or loose unknown-key handling, and why?

level: principalimportance: should knowfreq 30%

answer

  1. no global switch to lean on
  2. direction of the data matters
  3. silent loss versus brittle rejection
  4. deploy order under strict
  5. enforce through a shared module

basics

~20 s

No single mode fits. A common policy: z.strictObject for write bodies from clients you redeploy, plain z.object for third-party responses and outbound views, z.looseObject only for forwarded data. Zod has no global switch, so enforce it by convention.

solid answer

~50 s

I would set the mode by the direction of the data, not one default for everything. For inbound write bodies from our own clients, `z.strictObject` turns a misspelt field into a 4xx instead of a silent no-op, at the cost of a deploy rule: the server must learn a new field before any client sends it. For third-party API responses, plain `z.object` strip mode, because a provider adding a field must not take us down. For outbound views, strip again, so parsing a database row with the response schema drops anything not declared. `z.looseObject` only for proxying payloads we do not own. Zod 4 has no `z.config()` option for unknown keys, the mode does not cascade into nested objects, and derived schemas inherit their base's mode, so I would enforce the policy through shared base schemas and a lint rule rather than hope.

go deeper

for a junior

Know the three builders and what each does with an extra key, so you can follow whichever convention your team has set.

for a middle

Explain why a schema for a third-party response and a schema for your own write endpoint might deliberately use different unknown-key modes.

for a senior

Point out how the policy leaks: nested objects need their own mode, derived schemas inherit a base's mode, and stripped object options overlap in unions.

for a principal

Own the trade between silent data loss and brittleness under version skew, write down the deploy order strict mode implies, and enforce the choice through shared bases and linting.

## The decision on the table Every Zod 4 object schema picks one way to treat **unknown keys**, properties the schema does not declare: plain `z.object` strips them silently, `z.strictObject` rejects them with an `unrecognized_keys` issue, and `z.looseObject` passes them through unvalidated. Individually each choice is easy. The lead's question is what the **team default** should be, because hundreds of schemas will copy whatever the first few did, and the library will not enforce a choice for you. ## What each mode costs over time | Mode | What goes wrong | Who notices | |---|---|---| | strip (`z.object`) | a client's misspelt or renamed field is dropped and the request still succeeds | nobody: no issue, no log, no count of removed keys | | strict (`z.strictObject`) | a client sending a field the server does not know yet gets a validation failure | the client, immediately, often during a deploy | | loose (`z.looseObject`) | unvalidated extras flow into business code typed as `unknown` | whoever finally reads one of them | Strip trades visibility for availability; strict trades availability for visibility. Loose gives up validation for the keys it keeps, which is rarely what a service boundary wants. ## Deciding by direction A policy that holds up usually splits by where the data comes from and goes: 1. **Inbound write bodies from clients you deploy yourself.** Strict is attractive here. A misspelt field becomes an error a developer sees on the first try, and a probe for a writable `role` column is rejected rather than quietly ignored. The standing cost is **deploy ordering**: when frontend and backend ship separately, the server must accept a new field before any client sends it, or open tabs on a newer build start failing. 2. **Responses from third-party APIs.** Strip. A provider adding a field to its response is normal evolution, and a strict schema would turn it into an outage. You still get typed, validated data for the fields you declared. 3. **Outbound responses you build.** Strip. Parsing the row or domain object through the response schema before sending it removes anything undeclared, such as an internal flag added to the table later. 4. **Pass-through and proxy code.** Loose, documented as an exception, because the job is to forward what you do not own. Some teams choose strip everywhere for simplicity and accept the silent loss; that is defensible when clients are external and version skew is constant. What is not defensible is leaving the choice to whoever writes each schema. When one schema is shared between client and server, the mode travels with it. A client that parses its own request body against a strict schema catches a stray field before the request leaves, which makes strict cheaper to adopt when both sides live in one repository, though separate deploys still need the ordering rule above. ## Why Zod makes this a policy problem, not a setting - **No global switch.** `z.config()` covers error-map, locale and JIT behaviour, not an unknown-key default, so the mode lives in every schema's source. - **No cascade.** A strict outer object still strips inside a nested `z.object`; a strict policy has to reach every nested object. - **Inheritance through derivation.** `.pick()`, `.omit()`, `.partial()` and `.extend()` keep the base schema's mode, so one wrongly built base silently sets the mode for every view derived from it. - **Unions interact.** Under strip, object options in a plain union can overlap and the first match wins; a strict narrower option or a discriminated union restores exclusivity. ## Making the policy stick - Publish shared building blocks, for example a module whose request schemas start from `z.strictObject` and whose external-response schemas start from `z.object`, and derive views from those bases. - Add a lint rule or review checklist item that flags a bare `z.object` in request-schema folders and a `z.looseObject` anywhere without a comment. - Record the deploy rule that strict implies: add a field on the server first, let clients send it in the same or a later release, and remove a field from clients before the server stops accepting it. - Revisit the decision when the audience changes. A schema consumed by clients you cannot redeploy pushes inbound handling towards strip with explicit versioning. Which HTTP status a rejected body earns, and how the issues are shaped into a response, are separate decisions made elsewhere; the unknown-key mode only decides whether there is an issue at all.

  • Under a strict-inbound policy, how do you ship a new optional field when frontend and backend deploy separately?
    Expand, then use. Release the backend first with the field added to the `z.strictObject` schema, so it accepts but does not require it. Then release the frontend that sends it. Reversing the order means every request from the new frontend that carries the field fails with `unrecognized_keys` until the backend catches up.
  • Your team chose strip everywhere. How do you recover some of the visibility strict mode would have given?
    Run the same input through a `z.strictObject` built from the schema's shape in a non-blocking check, and log or count the `unrecognized_keys` issues while the real parse strips. That surfaces client typos and drift without rejecting requests, and it tells you when switching an endpoint to strict is safe.

  • Expects to set the unknown-key mode once for the whole app through z.config().
  • Proposes strict schemas for third-party API responses without weighing the outage risk.
  • Assumes a strict outer request schema also locks down its nested objects.
  • Treats strip mode as free, ignoring that client typos disappear without any signal.
  • Picks z.looseObject as a team default because it never rejects a request.

Assumes Zod 4; the modes are chosen with z.object, z.strictObject and z.looseObject (Zod 3's .strict() and .passthrough() remain as legacy methods), and z.config() has no unknown-key setting.