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

Making coerce to be a type argument #1793

Closed

Conversation

RobinTail
Copy link
Contributor

@RobinTail RobinTail commented Jan 1, 2023

Fixes #1760

This PR makes the schema types, that support coerce feature, to have the correct type of the input schema if this feature is enabled.

Thus, z.coerce.number() will have the type compatible to ZodType<number, ZodNumberDef, any>.
Third type argument stands for input type, which in case of coerce accepts anything.

Therefore, taking z.input<> on such schema will return any.

The behavior of schemas and methods without coerce feature, of having that feature disabled, remains the same.

Thanks to the default values of the new type argument, this change is backward compatible.

@netlify
Copy link

netlify bot commented Jan 1, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 642602d
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/63b54fd2fdf6fb000822e32d
😎 Deploy Preview https://deploy-preview-1793--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@RobinTail
Copy link
Contributor Author

The test with Deno fails due to some issue within the master branch

@RobinTail
Copy link
Contributor Author

Should be fixed after merging #1794

@RobinTail RobinTail force-pushed the fix-types-with-coercion branch from b1c2efd to f64c0b2 Compare January 2, 2023 13:58
@RobinTail RobinTail marked this pull request as ready for review January 4, 2023 10:08
@RobinTail
Copy link
Contributor Author

@JacobWeisenburger || @colinhacks , please review

@JacobWeisenburger
Copy link
Contributor

Unfortunately, I don't think I have enough experience to approve this. Perhaps @colinhacks or @scotttrinh would be able to help.

@RobinTail
Copy link
Contributor Author

Ok. I updated the description for the clarity.

@RobinTail
Copy link
Contributor Author

@colinhacks || @scotttrinh , please review

@RobinTail
Copy link
Contributor Author

image

@aleclarson
Copy link

aleclarson commented Jan 16, 2023

Why not create a new ZodCoercedString type (plus the others) instead of complicating the existing types?

Also, I don't think any is the correct type to return from z.input. Although, it's probably fine until TypeScript gets negated types?

@colinhacks
Copy link
Owner

Discussed this under the original issue: #1760 (comment)

@stale
Copy link

stale bot commented May 10, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No activity in last 60 days label May 10, 2023
@stale stale bot closed this Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale No activity in last 60 days
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inferring type with zod.input when using zod.coerce 3.20: Input type of a schema with a coercion
4 participants