feat(zod): support defs at root level in json schema converter#1649
Conversation
|
Run failed. View the logs →
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reviewed changes — adds opt-in root-level
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes – the follow-up commit addressed the $defs-hoisting concern from the prior review and expanded the happy-path test to cover nested named schemas.
packages/zod/src/converter.tsnow reads the schema id fromthis.options.metadata ?? globalRegistry, spreads any existing$defsonto the outer$defs, and avoids overwriting an existing entry with!(id in $defs).packages/zod/src/converter.test.tsexercises the nested case: an object schema with nameda/bproperties and arootid produces a flat$defsmap with correct local$refpointers.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
More templates
@orpc/arktype
@orpc/bun
@orpc/client
@orpc/cloudflare
@orpc/contract
@orpc/experimental-effect
@orpc/evlog
@orpc/json-schema
@orpc/nest
@orpc/next
@orpc/openapi
@orpc/opentelemetry
@orpc/pino
@orpc/publisher
@orpc/ratelimit
@orpc/server
@orpc/shared
@orpc/tanstack-query
@orpc/valibot
@orpc/zod
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@KingSora Just update logic and tests, if y have time please review it again! |
There was a problem hiding this comment.
ℹ️ No critical issues — minor suggestion inline.
Reviewed changes — the latest improve commit tightened id-collision behavior, removed the defensive try/catch, and expanded test coverage for the root $ref/$defs workaround.
packages/zod/src/converter.tsno longer swallows exceptions in the root workaround; instead, when the schema id collides with an existing$defsentry, the root definition is renamed to<id>__<index>.packages/zod/src/converter.test.tsnow module-mockszod/v4/coreto stubtoJSONSchemaand splits the single root test into three cases covering the global registry, a custom metadata registry, and id-collision handling.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
|
@dinwwwh Cool :) Thats looks nice, I only have one note: I've been also thinking about implementing the re-naming strategy for colliding ids but I didn't do it because zods Edit: You can look it up here: https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/to-json-schema.ts#L227-L240 |
There was a problem hiding this comment.
✅ No new issues found for this delta.
Reviewed changes — the latest commit adds JSON Pointer escaping for root definition ids, so ids containing ~ or / produce valid $ref pointers while keeping the raw id as the $defs key.
packages/zod/src/converter.tsnow encodes the root definition name withencodeJsonPointerSegmentbefore interpolating it into the$refpointer.packages/zod/src/converter.test.tsexercises the escaping path with an id of'root~/', expecting#/$defs/root~0~1.
Kimi K2 (free via Pullfrog for OSS) | 𝕏

This is a small workaround to support root level
$refand$defsinZodToJsonSchemaConverteruntil colinhacks/zod#6026 gets merged eventually.Note: the
try catchblock is just here as a defensive mechanism to guard against exceptions, but I couldn't encounter any case where an exception was encountered.