Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shallow: boolean to parse method options #1769

Open
evgeniyPP opened this issue Dec 28, 2022 · 2 comments
Open

Add shallow: boolean to parse method options #1769

evgeniyPP opened this issue Dec 28, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@evgeniyPP
Copy link

I want to use zod and immer together, but zod's parse method makes a deep clone of the passed variable and messes up immer's produce method result.

Can we add a shallow: boolean option to parse methods options to avoid this? Or a deep clone is absolutely necessary?

@evgeniyPP evgeniyPP changed the title Add shallow: boolean to parse method options Add shallow: boolean to parse method options Dec 28, 2022
@JacobWeisenburger JacobWeisenburger added the enhancement New feature or request label Dec 28, 2022
@santosmarco-caribou
Copy link
Contributor

santosmarco-caribou commented Jan 4, 2023

@evgeniyPP thanks for submitting this issue.

My personal opinion, though, is that this is not so easy to implement, and I don't think this lines up with Zod's principles.

Each one of Zod's subclasses implements its own _parse method, which is what controls how the parsing will work for this class specifically. Zod deep clones the input value because ZodArray computes each value individually, as well as ZodTuple, ZodObject etc. Adding a shallow option to parse() means adding a check to each one of the subclasses to tell them not to compute each value separately and instead iterate through the items/properties of the input without reassigning them to a result variable.

This will require adding checks to many subclasses, and then people extending Zod with their own subclasses will have to worry about this as well.

Additionally, some types of input require cloning, e.g., functions when parsed with ZodFunction, or Promises w/ ZodPromise. Parsing here happens on a secondary moment (when the function gets called/Promise gets resolved). I'm not sure there's a clear way of making this shallow option work reliably for all types.

But, well, as always, feel free to open a PR if you feel strong about this. PRs are always welcomed.

@bvallee-thefork
Copy link

Just cross referencing discussions, here is a comment about providing a .validate() function only doing validation and no cloning: #205 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants