Skip to content

feat(zod,valibot,arktype): add option to cache generated JSON schemas - #1753

Merged
dinwwwh merged 1 commit into
middleapi:mainfrom
dinwwwh:claude/cache-json-schema-weakmap-cdcdbe
Jul 30, 2026
Merged

feat(zod,valibot,arktype): add option to cache generated JSON schemas#1753
dinwwwh merged 1 commit into
middleapi:mainfrom
dinwwwh:claude/cache-json-schema-weakmap-cdcdbe

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Jul 30, 2026

Copy link
Copy Markdown
Member

Adds an opt-in cache option to ZodToJsonSchemaConverter, ValibotToJsonSchemaConverter, and ArkTypeToJsonSchemaConverter that memoizes conversion results in per-direction WeakMaps keyed by the schema instance. Converting the same schema repeatedly — e.g. regenerating an OpenAPI spec or sharing a converter across plugins — no longer re-runs the underlying JSON schema conversion.

Performance

  • Repeat conversions of the same schema instance return the cached [jsonSchema, optional] tuple with no conversion work.
  • input and output directions are cached independently, and WeakMap keys keep schemas garbage-collectable.

Notes

  • Disabled by default: cached results are shared objects, so they must be treated as immutable when the option is enabled.
  • The cache flag is stripped before options are forwarded to the underlying toJsonSchema implementations.

Testing

  • New tests in all three packages verify results are reused per schema and direction, the underlying conversion runs only once, and behavior is unchanged when disabled.
  • Docs for the zod, valibot, and arktype integrations mention the new option.

Adds an opt-in `cache` option to ZodToJsonSchemaConverter,
ValibotToJsonSchemaConverter, and ArkTypeToJsonSchemaConverter that
memoizes conversion results in per-direction WeakMaps keyed by the
schema instance, so repeated conversions of the same schema are free.
@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
orpc Ready Ready Preview Jul 30, 2026 6:48am

@pkg-pr-new

pkg-pr-new Bot commented Jul 30, 2026

Copy link
Copy Markdown
More templates

@orpc/ai-sdk

npm i https://pkg.pr.new/@orpc/ai-sdk@1753

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@1753

@orpc/bun

npm i https://pkg.pr.new/@orpc/bun@1753

@orpc/client

npm i https://pkg.pr.new/@orpc/client@1753

@orpc/cloudflare

npm i https://pkg.pr.new/@orpc/cloudflare@1753

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@1753

@orpc/experimental-effect

npm i https://pkg.pr.new/@orpc/experimental-effect@1753

@orpc/evlog

npm i https://pkg.pr.new/@orpc/evlog@1753

@orpc/json-schema

npm i https://pkg.pr.new/@orpc/json-schema@1753

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@1753

@orpc/next

npm i https://pkg.pr.new/@orpc/next@1753

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@1753

@orpc/opentelemetry

npm i https://pkg.pr.new/@orpc/opentelemetry@1753

@orpc/pinia-colada

npm i https://pkg.pr.new/@orpc/pinia-colada@1753

@orpc/pino

npm i https://pkg.pr.new/@orpc/pino@1753

@orpc/publisher

npm i https://pkg.pr.new/@orpc/publisher@1753

@orpc/ratelimit

npm i https://pkg.pr.new/@orpc/ratelimit@1753

@orpc/server

npm i https://pkg.pr.new/@orpc/server@1753

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@1753

@orpc/swr

npm i https://pkg.pr.new/@orpc/swr@1753

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@1753

@orpc/trpc

npm i https://pkg.pr.new/@orpc/trpc@1753

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@1753

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@1753

commit: 75c2b5f

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing dinwwwh:claude/cache-json-schema-weakmap-cdcdbe (75c2b5f) with main (df573c1)

Open in CodSpeed

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — a consistent, clean addition of an opt-in cache option to the three schema converters:

  • cache option on ZodToJsonSchemaConverterOptions — constructor strips cache before forwarding params; convert() checks per-direction WeakMap before calling extracted convertUncached().
  • cache option on ValibotToJsonSchemaConverterOptions — same pattern; constructor field renamed optionsconversionConfig.
  • cache option on ArkTypeToJsonSchemaConverterOptions — same pattern.
  • Tests — verify reference-equality cache hits, direction independence, call-count on the underlying library function, and disabled passthrough across all three packages.
  • Docs::: tip blocks with immutability warning.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@dinwwwh
dinwwwh merged commit 21251bb into middleapi:main Jul 30, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant