Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'.