Repository navigation
Add lazySchemas option for on-demand model namespaces - #105
Merged
Merged
Conversation
With tsNamespaces enabled, importing a models module built every Zod schema of the namespace at import time, even when the importer needed a single one. The new lazySchemas option emits each schema as a memoized module-level factory, exports the namespace as an object of getters and keeps the inferred types in a merged `declare namespace`, so schemas are built on first access. Common-model proxies forward to the shared namespace through getters as well. Both the TypeScript generator and the native renderer emit identical output; the option is rejected without tsNamespaces. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RH7NW9diCa2nftiQTsmp1F
urbankrepel-povio
pushed a commit
that referenced
this pull request
Oct 23, 2026
Merging main brought in #105, whose lazy-schema helpers still took the property reference names as `&HashMap<&str, String>`. This branch replaced that map with `PropertyRefNames` (built once per render), so the native crate didn't compile on the PR's merge ref. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
Requested by Urban · project thread
Summary
Adds a
lazySchemasoption (--lazySchemas, requirestsNamespaces) that builds each model schema on first access instead of at import.With
tsNamespaces, importing a models module today constructs every Zod schema of the namespace, even when the importer needs one of them. On large APIs that is a measurable share of frontend cold start (the TOS frontend spends tens of milliseconds per details route constructing schemas it never reads). WithlazySchemas: truethe generator emits:PetModels.PetSchemais a regular Zod schema (.extend,.shape,.pickwork) and the same instance on every access.PetModels.Petkeeps resolving as a type through the mergeddeclare namespace; circular schemas keep their explicit type andz.ZodObject<z.ZodRawShape> & z.ZodType<...>annotation.PetModels.Status) are lazy getters too.modelsInCommonproxies forward to the common namespace through getters.CategorySchema()); member accesses, object keys, string and.regex(/…/)literals are left alone.compileSchemasstill wraps non-circular factories inz.compile(...).Changes
options.ts,options.const.ts,generate.command.ts,resolveConfig.ts(throwslazySchemas requires tsNamespaces),native/src/config.rs.generateModels.ts(renderLazyModels, lazy proxy) andgenerate.lazy.utils.ts(sibling-call rewriter,oncehelper lines).render.rs(render_lazy_common_models,render_lazy_schema_parts,call_lazy_schema_references, lazyrender_proxybranch). Output is byte-identical to the TypeScript generator.lazySchemasis a 7th layout key (128 layouts, 56 invalid, 72 runtime-checked).generateModels.test.ts; config validation inresolveConfig.test.ts.Test plan
bun run build:nativebun run test:ts(1327 passed)bun run test:native(1327 passed)bun run typecheck,bun run lint:check,bun run formattsc --strictand runs under bun (circular parse, enum access,.extend, shared identity)Generated by Claude Code