zod icon indicating copy to clipboard operation
zod copied to clipboard

feat: `strict` option for transforming overflow properties to value

Open drakedeatonuk opened this issue 1 year ago • 1 comments

It would be handy if ZodObject's strict function accepted a parameter that let you transform any overflowing properties to a specific value rather than simplify removing them.

For example:


const user = {
  firstName: "foo",
  lastName: "bar",
} ;

const filteredUserSchema = z.object({
  firstName: z.string(),
}).strict({
  castOverflowTo: "[filtered]"
});

// castOverflowTo would result in the parsed return looking like:
// { 
//    firstName: "foo",
//    lastName: "[filtered]"
// }
filteredlUserSchema.safeParse(user).data

This would be very useful for scrubbing data.

Thanks in advance!

drakedeatonuk avatar Sep 24 '24 19:09 drakedeatonuk

Hi, @drakedeatonuk. I'm Dosu, and I'm helping the Zod team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You proposed enhancing ZodObject with a strict option to transform overflowing properties to a placeholder value like "[filtered]".
  • This feature is intended to aid in data scrubbing by replacing unwanted properties.
  • You provided an example to illustrate the proposed functionality.
  • There has been no activity or comments on the issue since it was opened.

Next Steps:

  • Please let us know if this issue is still relevant to the latest version of the Zod repository. You can keep the discussion open by commenting on the issue.
  • If there is no further activity, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Jun 17 '25 16:06 dosubot[bot]

This is achievable with .catchall() which accepts an arbitrary schema that will process unrecognized properties.

colinhacks avatar Jul 24 '25 19:07 colinhacks