-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
z.date({ coerce: 'iso' }) #3646
base: v4
Are you sure you want to change the base?
Conversation
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Closes #3643
I got one whole 👍 on that issue and I think it's a good idea so opening a PR.
hi - I find
z.date({ coerce: true })
very useful when using with trpc or another tool that uses JSON to cross an i/o boundary. Because, it forces the client to use aDate
value - which is then serialized via itstoJSON
method into an ISO string and correctly converted back to aDate
on the other side. So you get niceDate
types on either side of the boundary.But a not-ideal effect of using that trick is that it will also accept values like
null
and0
which both parse to1970-01-01T00:00:00.000Z
, so it's a bit dangerous.The change here is to change
coerce: boolean
tocoerce: boolean | 'iso'
, then update this block:zod/src/types.ts
Lines 1798 to 1800 in 9257ab7
This PR makes that type change, and implements it by using new Date(...) and checking that the original input exactly matches the resultant .toISOString() value.
It also adds some tests to 1) highlight the danger of coerce: true and 2) make sure everything works sensibly with coerce: 'iso'.