You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(zod,valibot,arktype): add option to cache generated JSON schemas (#1753)
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.
Copy file name to clipboardExpand all lines: apps/content/docs/integrations/arktype.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,6 +41,15 @@ const generator = new OpenAPIGenerator({
41
41
})
42
42
```
43
43
44
+
::: tip
45
+
Enable the `cache` option to reuse conversion results when the same schema instance is converted repeatedly. When enabled, repeated conversions return the same JSON schema object, so treat the results as immutable.
A common pattern is defining reusable or recursive types using scopes. The converter preserves them in `$defs`, which `OpenAPIGenerator` can then [hoist](/docs/openapi/specification#hoisting-defs) into `components.schemas`.
Copy file name to clipboardExpand all lines: apps/content/docs/integrations/valibot.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,6 +41,15 @@ const generator = new OpenAPIGenerator({
41
41
})
42
42
```
43
43
44
+
::: tip
45
+
Enable the `cache` option to reuse conversion results when the same schema instance is converted repeatedly. When enabled, repeated conversions return the same JSON schema object, so treat the results as immutable.
A common pattern is defining reusable or recursive schemas via definitions. The converter preserves them in `$defs`, which `OpenAPIGenerator` can then [hoist](/docs/openapi/specification#hoisting-defs) into `components.schemas`. For more on how definitions work in Valibot, see [Valibot JSON Schema Definitions](https://github.com/open-circle/valibot/blob/main/packages/to-json-schema/README.md#definitions).
Copy file name to clipboardExpand all lines: apps/content/docs/integrations/zod.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,15 @@ const generator = new OpenAPIGenerator({
45
45
})
46
46
```
47
47
48
+
::: tip
49
+
Enable the `cache` option to reuse conversion results when the same schema instance is converted repeatedly. When enabled, repeated conversions return the same JSON schema object, so treat the results as immutable.
A common pattern is defining reusable schemas with `id` metadata. The converter places them in `$defs`, which `OpenAPIGenerator` then [hoists](/docs/openapi/specification#hoisting-defs) into `components.schemas`. For more on `id` and `$ref` in Zod, see [Zod JSON Schema Registries](https://zod.dev/json-schema?id=registries#registries).
0 commit comments