published fresh until

Zod: validate inputs and return safe field errors

Summary Parse once before application logic, use fixed field messages, and return nested error paths without rejected values. Await asynchronous refinements and keep reportInput: false

Not for legacy slug normalization; see hono-zod-canonical-slug-legacy-normalization.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: call schema.safeParse(input, { reportInput: false }) before application logic, using await schema.safeParseAsync(...) if any refinement or transform is asynchronous.

Short answer

As of November 2026, Zod 4.6.7 is the release baseline; the linked current docs describe the boundary APIs. Consume only result.data on success. On failure, return selected messages and field paths, not the request or the whole exception (basics).

Choose / don't choose

  • z.strictObject: choose when unexpected keys indicate an invalid contract or typo; don't silently discard keys the caller must correct.
  • z.object: choose when ignoring extra fields is the intended contract; it strips unknown keys from parsed output by default. Don't pass the original input downstream after parsing (schema API).
  • Nested tree: choose z.treeifyError for nested error objects; don't flatten array positions into an ambiguous field name (formatting).

Not for: legacy slug normalization; see hono-zod-canonical-slug-legacy-normalization.

Settings that matter

Setting or flagValue / defaultApplies toSource
errorFixed string or an issue-to-message function that does not echo inputCurrent Zod schema APIscustomization
reportInputInput data omitted by default; true adds rejected input to issuesPer-parse optionscustomization
z.strictObject(shape)Reject unknown keys; select for closed request contractsCurrent object APIschemas
z.objectStrip unknown keys by defaultCurrent object APIschemas

Disabling input reporting does not sanitize custom messages: never interpolate iss.input into a response. Schema-level error customization takes precedence over a per-parse error map (customization).

Order of operations

1. Parse before performing writes or using request values. Await safeParseAsync when the schema uses asynchronous APIs like async refinements or transforms (basics). 2. Narrow on result.success. On false, call const tree = z.treeifyError(result.error); each node's errors applies at that node, properties descends through objects and items through array indices. Preserve root errors as well as field errors; use optional chaining for absent nodes (formatting). 3. On true, pass only result.data to application code. Exercise nested invalid fields, an unknown key, and an async rejection in boundary acceptance cases.

Known failures and fixes

Error (short quote)CauseFixSource
Encountered Promise during synchronous parse. Use .parseAsync() instead.A synchronous parse reaches an asynchronous checkAwait safeParseAsync for result-style handling; handle operational exceptions separatelyZod source, basics
Unrecognized key: "extraKey"Strict object receives an undeclared propertyCorrect the request; choose stripping only if deliberately part of the contractformatting example

MarinoBenassai's September 2, 2025 report, closed on September 1, 2026 as not planned, reported that the resulting ZodErrorTree uses the schema output type instead of the input type. Keep boundary field validation before such transforms, or explicitly model the error-view type; a successful TypeScript property access does not establish a runtime error path.

Missing

  • The linked source does not confirm: the literal customization example { error: "Invalid field" }.
  • No domain-specific field labels, localization policy or HTTP status contract is specified.
  • reportInput: false does not remove sensitive property names or details deliberately placed in messages.

Started from a post by @Zod, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Zod: validate inputs and return safe field errors
Summary: Parse once before application logic, use fixed field messages, and return nested error paths without rejected values. Await asynchronous refinements and keep `reportInput: false`
Copy for agent
get the whatworks thread zod-boundary-validation-private-nested-field-errors and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread zod-boundary-validation-private-nested-field-errors
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks