fix(schema-to-json): support serializing Zod 4 z.date() to JSON Schema - #4962
kaiizer777 wants to merge 1 commit into
Conversation
triggerdotdev#4939) In Zod 4, toJSONSchema delegates Date schemas to handleUnrepresentable, which threw 'Date cannot be represented in JSON Schema' when unrepresentable was omitted. Configure unrepresentable to return { type: 'string', format: 'date-time' } for date schemas to match Zod 3 output while preserving strict error handling for other unrepresentable types.
🦋 Changeset detectedLatest commit: ccad17a The changes in this PR will be included in the next version bump. This PR includes changesets to release 27 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Hi @kaiizer777, thanks for your interest in contributing! This project requires that pull request authors are vouched, and you are not in the list of vouched users. This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details. |
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
| unrepresentable: ({ zodSchema }) => { | ||
| const def = (zodSchema as any)._zod?.def; | ||
| if (def?.type === "date") { | ||
| return { type: "string", format: "date-time" }; | ||
| } | ||
| return "throw"; | ||
| }, |
There was a problem hiding this comment.
🟡 Older Zod versions weaken schemas
With Zod 4.0–4.4, the unrepresentable function disables throwing for non-date types. Those releases only compare the option with "throw", so unsupported schemas become {} and accept rejected values.
Learn more
The package accepts Zod ^4.0.0, but function-valued unrepresentable handlers were added only in later Zod 4 releases. Earlier releases treat every value except the literal "throw" as permissive behavior. The callback therefore never runs there, although the existing override still converts dates. Other unrepresentable nodes lose their previous errors and emit unconstrained schemas.
Example: With Zod 4.1, converting z.symbol() previously throws. After this change it emits {}, which permits strings and numbers even though z.symbol() rejects them.
Recommended fix: Either raise both Zod dependency ranges to the first version supporting UnrepresentableHandler, or preserve compatibility by detecting that capability and explicitly throwing from override for every non-date unrepresentable node on older releases. Add a test installed against the minimum supported Zod 4 version.
Was this helpful? React with 👍 or 👎 to provide feedback.
Fixes #4939
Problem
In
@trigger.dev/schema-to-json, converting a Zod 4 schema that includesz.date()threw an uncaught error:This crashed MCP
tools/listresponses as well as schema generation/indexing when task payloads or tools utilizedz.date().Root Cause
convertZod4Schemainpackages/schema-to-json/src/index.tscalledz4.toJSONSchema(schema, ...)without configuring anunrepresentablehandler. In Zod 4,dateProcessorinvokeshandleUnrepresentable, which defaults to"throw".Solution
unrepresentableinz4.toJSONSchemato mapdatenodes to{ type: "string", format: "date-time" }, achieving parity with Zod 3 (zod-to-json-schema)."throw"behavior for other unrepresentable types (e.g.undefined,symbol).dateformatting inoverrideto ensure schema consistency.z.date()standalone and nested in objects with optional/nullable/array wrappers across both Zod 4 and minimum Zod 3 permalink.@trigger.dev/schema-to-json.Verification
pnpm --filter "@trigger.dev/schema-to-json" run test -- --run(25 passed)pnpm --filter trigger.dev run test src/mcp/schemas.test.ts -- --run(20 passed)pnpm run build --filter "@trigger.dev/schema-to-json"pnpm exec oxfmt packages/schema-to-json/src/index.ts packages/schema-to-json/tests/index.test.tspnpm exec oxlint packages/schema-to-json/src/index.ts packages/schema-to-json/tests/index.test.ts