Skip to content

Feature request: z.deepStrict(), like z.deepPartial() for unknown keys #6721

Open

Description

@bvandrc

.strict() and z.strictObject() only cover the top-level shape, so nested objects still silently strip keys they don't declare:

const Post = z.strictObject({ author: z.object({ name: z.string() }) });

Post.parse({ author: { name: "Ann", age: 30 } }); // ✅ `age` is silently dropped

Making every nested object strict by hand means rebuilding the schema, and it doesn't work for schemas you didn't write.

Proposed: z.deepStrict(schema), built the same way as z.deepPartial(): walk the schema with visit and make every object in the tree strict, through arrays, tuples, unions, intersections, records, wrappers, and z.lazy.

z.deepStrict(Post).parse({ author: { name: "Ann", age: 30 } }); // ❌ unrecognized key "age"

Intersections need no special case, since v4 already reconciles unrecognized_keys across both sides.

One design question: should an object that already sets its own unknown-key handling (z.looseObject(), .catchall()) keep it? I'd say yes, since a nested looseObject is a deliberate choice, though that differs from .strict(), which overrides a catchall.

This would be tree-shakeable like z.deepPartial(), with no cost unless imported.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions