Skip to content

docs: five examples in api.mdx and codecs.mdx throw or contradict their comments #6731

Open

Description

@superbot-helper

Five examples in packages/docs/content/api.mdx and packages/docs/content/codecs.mdx are wrong when run against zod@4.6.7. They fail in different ways: a callback context that is not the type the comment names, input that never reaches the refinement it is meant to demonstrate, a sync parse of an async transform, a date literal whose result depends on the machine timezone, and a section that throws on zod/mini. They are separate fixes. A patch for all five is at https://gist.github.com/superbot-helper/318386029f96b5af91b5991d957db658 and applies to main at 93b7a62 with git apply.

Every snippet below was executed against zod@4.6.7 with TZ=America/Chicago, before and after the patch. The output is pasted as it printed.

.catch() hands the callback a plain object, not a ZodError

const numberWithRandomCatch = z.number().catch((ctx) => {
  ctx.error; // the caught ZodError

  return Math.random();
});

The callback context is { value, issues, error, input }. ctx.error is the deprecated { issues } object, and z.ZodError is not one of its types:

ctx keys: [ 'value', 'issues', 'error', 'input' ]
ctx.error instanceof z.ZodError: false
ctx.error constructor: Object

ctx.issues is the array the callback is handed, and the Zod Mini tab of the same block already shows it:

Array.isArray(ctx.issues): true
ctx.issues[0].code: invalid_type

#5454 reported this and was closed in December 2025, but the line is unchanged at HEAD and on https://zod.dev/api#catch. The patch changes it to ctx.issues; // the caught validation issue.

The when example's password never reaches when

Both tabs pass password: "asdf" to a field with an 8 character minimum (.min(8) in the Zod tab, .check(z.minLength(8)) in the Mini tab), so the password fails before the refinement runs. when returns false and no confirmPassword issue is produced, contradicting the comment on the same input in the Zod tab. The Zod Mini tab's comment says the opposite of the Zod tab's, "will prevent".

schema.parse({
  password: "asdf",
  confirmPassword: "asdf",
  anotherField: 1234 // this error will not prevent the password check from running
});
thrown: ZodError
issues: [{"code":"too_small","path":["password"],"message":"Too small: expected string to have >=8 characters"},{"code":"invalid_type","path":["anotherField"],"message":"Invalid input: expected string, received number"}]

With an eight character password the documented claim holds:

thrown: ZodError
issues: [{"code":"invalid_type","path":["anotherField"],"message":"Invalid input: expected string, received number"},{"code":"custom","path":["confirmPassword"],"message":"Passwords do not match"}]

The patch uses password: "asdfasdf" in both tabs and makes both comments say "will not prevent". #5805 fixed the .pick() call in this example but not the data.

The Zod Mini async transform calls the sync .parse()

const user = await idToUser.parse("abc123");

idToUser has an async transform, so this throws:

thrown: $ZodAsyncError: Encountered Promise during synchronous parse. Use .parseAsync() instead.

await does not help because .parse() is synchronous. The Zod tab above this one and the callout below the example both say .parseAsync. With that change:

resolved user: {"id":"abc123","name":"Ada"}

The codecs refinement example depends on the process timezone

codecs.mdx:233-236 encodes new Date("2000-01-01"), which is UTC midnight. In America/Chicago that is 1999-12-31 18:00, so getFullYear() returns 1999 and the documented success case throws:

thrown: ZodError
issues: [{"code":"custom","path":[],"message":"Must be this millennium"}]

The same snippet passes under TZ=UTC, so its result depends on where it runs. The // => Date comment is also wrong: encode returns the serialized string. Constructing both dates in local time works, and the second line still throws the documented error:

schema.encode(new Date(2000, 0, 1)); // => 2000-01-01T06:00:00.000Z
schema.encode(new Date(1999, 0, 1)); // => ZodError: Must be this millennium

The Prefaults section has no Zod Mini tab

The Prefaults section (api.mdx:3197-3221) has no <Tabs>, unlike the Defaults and Catch sections around it, and every snippet uses classic methods that zod/mini schemas do not have:

const schema = z.string().transform(val => val.length).prefault("tuna");
thrown: TypeError: z.string(...).transform is not a function

The top level z.prefault(schema, value), z._default(...), z.pipe(...), z.transform(...) and .check(z.trim(), z.toUpperCase()) forms exist and produce the documented values. The patch adds a Zod Mini tab to each of the three snippets, matching the Defaults and Catch sections. All six forms then run as documented:

default, Zod: 0
default, Zod Mini: 0
prefault, Zod: 4
prefault, Zod Mini: 4
mutating, Zod: "TUNA" "  tuna  "
mutating, Zod Mini: "TUNA" "  tuna  "

Patch

https://gist.github.com/superbot-helper/318386029f96b5af91b5991d957db658

2 files changed, 41 insertions, 8 deletions. It touches only packages/docs/content/api.mdx and packages/docs/content/codecs.mdx, no source files. git apply --check --whitespace=error-all and patch -p1 --dry-run both pass on a clean checkout of 93b7a62. packages/docs/content/api.test.ts passes on the patched api.mdx.

Activity

  1. ump45nose commented on Nov 5, 2026

    @ump45nose
    Contributor

    I put a docs-only fix for the five broken snippets on a fork branch:

    main...ump45nose:fix/6731-docs-examples

    Changes match the reproduction/patch in this issue (api.mdx + codecs.mdx only). Happy to open a PR if maintainers prefer that path — gh pr create from this account currently returns a permissions/404 error against this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions