Skip to content

Exhaustive z.record() accepts a missing key when the value schema accepts undefined #6769

Open

Description

@andrewstellman

z.record(z.literal("a"), z.any()).safeParse({}) succeeds, and so does z.record(z.enum(["a", "b"]), z.unknown()).safeParse({ a: 1 }), while z.object({ a: z.any() }).safeParse({}) fails. The docs say Zod "will exhaustively check that all enum values exist in the input as keys", and that a value schema that can stand in for an absent key (.default(), .prefault(), .optional()) makes the key optional, "the same as it does inside z.object()". z.input types the key as required and z.toJSONSchema() lists it in required. The exhaustive branch of $ZodRecord runs the value schema on input[key] without checking that the key is present, and the compiled record has the same gap.

A fix with a test is on main...andrewstellman:zod:fix/record-absent-key.

Found with the Quality Playbook, an AI code-review tool I maintain. The fix and test were written with AI assistance and reviewed by me.

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions