fix(typescript-zod): preserve null for nullable date-time fields#2966
Merged
Conversation
Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bug
The TypeScript-Zod renderer emitted nullable date-time fields as
z.union([z.coerce.date(), z.null()]). Zod tries union members in order, andz.coerce.date()happily coercesnullintonew Date(null)(the Unix epoch,1970-01-01T00:00:00.000Z) instead of failing, so thez.null()branch was never reached. A JSON field withcreatedAt: nullround-tripped as an epoch timestamp instead of stayingnull.Root cause
In
packages/quicktype-core/src/language/TypeScriptZod/TypeScriptZodRenderer.ts, the union-type case built thez.union([...])member list in whatever orderunionType.getChildren()returned them, with no regard for the fact that Zod's coercing schemas (likez.coerce.date()) will happily acceptnulland coerce it, silently shadowing az.null()member placed after it.Fix
Sort the union's children so
z.null()is emitted first in the generatedz.union([...])call. Verified directly against the installedzodpackage:Test coverage
test/inputs/json/priority/bug2590.json(added in #2879 for an unrelated Dart bug) already contains an object with a nullable date-time field, including an array element where it'snull. It was previously skipped fortypescript-zodintest/languages.tswith a comment pointing at this exact issue (// z.coerce.date() coerces null to the Unix epoch: #2880). This PR removes that skip entry so the fixture now runs fortypescript-zod, exercising this regression end-to-end (generate → compile → round-trip via zod'sparse/serialize).Verification
node dist/index.js -l typescript-zod sample.jsononbug2590.jsonemittedz.union([z.coerce.date(), z.null()]); confirmed at the zod runtime level that.parse(null)returned an epochDate.npm run buildpasses), and confirmed the generated code now emitsz.union([z.null(), z.coerce.date()]), with.parse(null)correctly returningnulland normal date-time strings still parsing toDateobjects.typescript-zodfixture suite locally:QUICKTEST=true FIXTURE=typescript-zod script/test— all 45/45 tests passed, includingbug2590.json.Fixes #2880
🤖 Generated with Claude Code