Skip to content

Add support for zod/mini generation - #3666

Merged
melloware merged 8 commits into
orval-labs:masterfrom
arthurfiorette:master
Jun 30, 2026
Merged

Add support for zod/mini generation#3666
melloware merged 8 commits into
orval-labs:masterfrom
arthurfiorette:master

Conversation

@arthurfiorette

@arthurfiorette arthurfiorette commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

This PR introduces zod/mini support so generated zod code is fully tree shakeable.

In large openapi schemas, simply by adding zod generator to the bundle could introduce 1-5Mb of code and more than 200mb of ram simply by importing the generated zod.

Inside cloudflare workers where memory is limited or even on any normal frontend where bundle size matters, this is a LOT.

Zod/mini (so zod is tree shakeable) alongside PURE comments (so generated code is tree shakeable) the final output stops mattering and any bundler can strip it all out during compilation process.

I did not make mini be the default option to not break existing users but I did change some examples around to use the new mini generator since most of the time sdk clients are used in frontends and in almost all occasions lower memory footprint & lower bundle size is great.

docs/content/docs/guides/zod.mdx kindly explains the tree shaking benefits, pls read it as well :)

Summary by CodeRabbit

Summary

  • New Features

    • Added override.zod.variant to generate Zod schemas in classic or mini style (default: classic).
    • Generated Zod schema files now use the correct import/API style for the selected variant.
  • Bug Fixes

    • Preserved variant through option normalization and reusable/recursive schema generation.
    • Added compatibility checks to prevent mini output with unsupported Zod targets.
    • Updated array-bounds, preprocess, and no-schema response generation to match the variant.
  • Documentation / Tests

    • Updated Zod guides/config references for mini and added regression tests for variant-specific output.

Copilot AI review requested due to automatic review settings June 29, 2026 22:52
@arthurfiorette
arthurfiorette marked this pull request as draft June 29, 2026 22:52
@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5a3ee620-48e2-4b2e-9730-0fd2f1dfd669

📥 Commits

Reviewing files that changed from the base of the PR and between 30bc5dc and 99ddae0.

📒 Files selected for processing (20)
  • docs/content/docs/guides/zod.mdx
  • docs/content/docs/reference/configuration/output.mdx
  • packages/angular/src/http-client.test.ts
  • packages/angular/src/http-resource.test.ts
  • packages/core/src/test-utils/context.ts
  • packages/core/src/types.ts
  • packages/hono/src/index.ts
  • packages/mcp/src/index.ts
  • packages/mock/src/faker/getters/combine.test.ts
  • packages/orval/src/reusable-schemas.test.ts
  • packages/orval/src/reusable-schemas.ts
  • packages/orval/src/utils/options.test.ts
  • packages/orval/src/utils/options.ts
  • packages/orval/src/write-zod-specs.test.ts
  • packages/orval/src/write-zod-specs.ts
  • packages/solid-start/src/index.test.ts
  • packages/zod/src/compatible-v4.test.ts
  • packages/zod/src/compatible-v4.ts
  • packages/zod/src/index.ts
  • packages/zod/src/zod.test.ts
✅ Files skipped from review due to trivial changes (4)
  • packages/core/src/test-utils/context.ts
  • packages/orval/src/reusable-schemas.test.ts
  • docs/content/docs/reference/configuration/output.mdx
  • packages/angular/src/http-resource.test.ts
🚧 Files skipped from review as they are similar to previous changes (14)
  • packages/mock/src/faker/getters/combine.test.ts
  • packages/mcp/src/index.ts
  • packages/zod/src/compatible-v4.test.ts
  • packages/core/src/types.ts
  • packages/angular/src/http-client.test.ts
  • packages/zod/src/compatible-v4.ts
  • packages/orval/src/reusable-schemas.ts
  • packages/orval/src/utils/options.ts
  • packages/solid-start/src/index.test.ts
  • packages/zod/src/zod.test.ts
  • packages/hono/src/index.ts
  • packages/orval/src/utils/options.test.ts
  • packages/orval/src/write-zod-specs.ts
  • packages/zod/src/index.ts

📝 Walkthrough

Walkthrough

Adds a ZodVariantOption (classic or mini) to Zod generation, normalizes it with a default of classic, validates mini against Zod v4, and threads the variant through schema parsing, file writing, reusable schemas, import generation, and docs.

Changes

Zod mini variant support

Layer / File(s) Summary
ZodVariantOption type and option normalization
packages/core/src/types.ts, packages/orval/src/utils/options.ts, packages/orval/src/utils/options.test.ts, packages/angular/src/http-client.test.ts, packages/angular/src/http-resource.test.ts, packages/core/src/test-utils/context.ts, packages/mock/src/faker/getters/combine.test.ts, packages/solid-start/src/index.test.ts
Declares ZodVariantOption, extends ZodOptions/NormalizedZodOptions, normalizes variant with a classic default, rejects per-operation variant, and updates test defaults to include classic.
assertZodTarget / getZodImportSource / getZodTypeName helpers
packages/zod/src/compatible-v4.ts, packages/zod/src/compatible-v4.test.ts, packages/zod/src/index.ts
Adds variant-aware target validation and mapping helpers, exports them from the Zod package, and updates dependency resolution to use the variant-specific import source.
parseZodValidationSchemaDefinition mini rendering
packages/zod/src/index.ts, packages/zod/src/zod.test.ts
Adds a variant parameter, implements the mini rendering path and preprocess wrapping, updates route generation to pass the variant through schema parsing, and expands tests for mini rendering and dependency resolution.
write-zod-specs.ts variant threading and import generation
packages/orval/src/write-zod-specs.ts, packages/orval/src/write-zod-specs.test.ts
Adds variant-aware schema import generation, threads variant through schema writers, validates Zod targets in each write path, and adds regression coverage for mini output.
reusable-schemas variant forwarding
packages/orval/src/reusable-schemas.ts, packages/orval/src/reusable-schemas.test.ts
Carries variant through reusable schema generation and emits /*#PURE*/ before recursive lazy schemas in mini mode.
hono and mcp variant-aware imports
packages/hono/src/index.ts, packages/mcp/src/index.ts
Replaces hardcoded Zod import statements with helper-generated variant-specific imports in both generators.
Zod variant docs updates
docs/content/docs/guides/zod.mdx, docs/content/docs/reference/configuration/output.mdx
Documents override.zod.variant, adds a Zod Mini section, and updates examples to show zod/mini usage.

Sequence Diagram(s)

sequenceDiagram
  participant writeZodSchemas
  participant assertZodTarget
  participant parseZodValidationSchemaDefinition
  participant generateZodSchemaFileContent
  participant getZodSchemaImportStatement
  writeZodSchemas->>assertZodTarget: validate variant and Zod v4
  writeZodSchemas->>parseZodValidationSchemaDefinition: pass zodVariant
  writeZodSchemas->>generateZodSchemaFileContent: render schema file content
  generateZodSchemaFileContent->>getZodSchemaImportStatement: choose zod import form
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • orval-labs/orval#3642: Both PRs modify the Zod option plumbing and the packages/zod/src/compatible-v4.ts / @orval/core type and normalization layer that controls Zod target selection.
  • orval-labs/orval#3467: Both PRs modify the reusable Zod schema rewrite pipeline in packages/orval/src/reusable-schemas.ts.
  • orval-labs/orval#3465: Both PRs modify Zod schema generation/import handling in packages/orval/src/write-zod-specs.ts.

Suggested labels

enhancement

Suggested reviewers

  • melloware

Poem

🐇 I hopped through classic, then found mini bright,
With zod/mini paths and checks that fit just right.
Pure little lazy hops now sparkle in the tree,
And Zod v4 keeps the doorway safe for me.
Thump-thump, the schemas bloom in tidy rows,
A rabbit’s code sings where the new variant goes.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding support for zod/mini generation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI 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.

Pull request overview

This PR adds a new Zod output variant (classic vs mini) so Orval can generate schemas targeting the Zod Mini API (zod/mini) and emit the corresponding “functional/check-based” schema expressions, while enforcing that Zod Mini generation only targets Zod v4.

Changes:

  • Introduces override.zod.variant (classic | mini) and normalizes it across Orval options.
  • Adds Zod target helpers (getZodImportSource, getZodTypeName, assertZodTarget) and updates schema/code generation + imports to support zod/mini.
  • Extends test coverage for Zod Mini rendering, dependency selection, and schema file output.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/zod/src/zod.test.ts Adds tests for Zod Mini rendering and dependency selection.
packages/zod/src/index.ts Implements Zod Mini rendering path, variant-aware dependencies, and variant propagation through route/schema generation.
packages/zod/src/compatible-v4.ts Adds helpers to validate Mini target (requires v4) and select import/type names by variant.
packages/zod/src/compatible-v4.test.ts Tests new Zod target helper behavior.
packages/orval/src/write-zod-specs.ts Emits variant-aware Zod imports and variant-aware schema type annotations for reusable schemas.
packages/orval/src/write-zod-specs.test.ts Verifies schema files use zod/mini imports and Mini-style output when configured.
packages/orval/src/utils/options.ts Normalizes override.zod.variant and prevents per-operation/tag overrides for it.
packages/orval/src/utils/options.test.ts Tests defaulting/preservation of variant and exclusion from operation/tag overrides.
packages/orval/src/reusable-schemas.ts Threads variant through reusable schema set generation options.
packages/mcp/src/index.ts Updates generated Zod schema file import to be variant-aware (zod vs zod/mini).
packages/hono/src/index.ts Updates generated Zod schema file import to be variant-aware (zod vs zod/mini).
packages/core/src/types.ts Adds ZodVariantOption and documents ZodOptions.variant; includes it in normalized options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/zod/src/index.ts
Comment thread packages/zod/src/index.ts

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/orval/src/write-zod-specs.ts (1)

906-943: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Add the missing Zod target assertion in reusable schema writing.

writeZodSchemas() returns into writeZodSchemasReusable() before its own assertion when generateReusableSchemas is enabled. This path resolves isZodV4 and passes variant through, but never rejects variant: 'mini' with non-v4 output, so it can emit zod/mini code for an invalid target.

Proposed fix
   const isZodV4 = resolveIsZodV4(
     output.override.zod.version,
     output.packageJson,
   );
+  assertZodTarget({ variant: output.override.zod.variant, isZodV4 });
   const strict = output.override.zod.strict.body;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/orval/src/write-zod-specs.ts` around lines 906 - 943, The reusable
schema path in writeZodSchemas/writeZodSchemasReusable is missing the Zod target
validation, so it can still proceed with variant: 'mini' on non-v4 output. Add
the same assertion used in the main Zod writing path before
generateReusableSchemaSet is called, using the existing isZodV4 and
output.override.zod.variant values to reject invalid mini targets early. Keep
the fix in writeZodSchemasReusable so reusable schema generation cannot emit
unsupported zod/mini output.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/zod/src/index.ts`:
- Around line 1329-1347: The allOf merge logic in the helper generation path is
concatenating each schema’s consts without a separator, which can collapse
adjacent exports into invalid output. Update the loop in the allOf handling
branch to insert a delimiter between each non-empty partSchema.consts chunk
before calling appendConstsChunk, so multiple emitted helpers remain
syntactically separated. Use the allOfArgs iteration and appendConstsChunk path
as the fix point.
- Around line 1432-1449: The tuple rendering in renderMiniDefinition is dropping
helper consts for tuple items and the optional rest schema, so generated
references can point to helpers that were never emitted. Update the tuple branch
in packages/zod/src/index.ts to collect and merge the consts from each item’s
renderMiniDefinition result, and do the same for the next[1] rest schema when
present, before returning the final tuple expression. Keep the logic localized
to the tuple handling path so zod.tuple([...], rest) still renders correctly in
mini mode.

---

Outside diff comments:
In `@packages/orval/src/write-zod-specs.ts`:
- Around line 906-943: The reusable schema path in
writeZodSchemas/writeZodSchemasReusable is missing the Zod target validation, so
it can still proceed with variant: 'mini' on non-v4 output. Add the same
assertion used in the main Zod writing path before generateReusableSchemaSet is
called, using the existing isZodV4 and output.override.zod.variant values to
reject invalid mini targets early. Keep the fix in writeZodSchemasReusable so
reusable schema generation cannot emit unsupported zod/mini output.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f72053ec-523d-4f56-9f54-ae6f77f92b26

📥 Commits

Reviewing files that changed from the base of the PR and between 4fd4803 and d2abeaa.

📒 Files selected for processing (12)
  • packages/core/src/types.ts
  • packages/hono/src/index.ts
  • packages/mcp/src/index.ts
  • packages/orval/src/reusable-schemas.ts
  • packages/orval/src/utils/options.test.ts
  • packages/orval/src/utils/options.ts
  • packages/orval/src/write-zod-specs.test.ts
  • packages/orval/src/write-zod-specs.ts
  • packages/zod/src/compatible-v4.test.ts
  • packages/zod/src/compatible-v4.ts
  • packages/zod/src/index.ts
  • packages/zod/src/zod.test.ts

Comment thread packages/zod/src/index.ts Outdated
Comment thread packages/zod/src/index.ts
@melloware melloware added the zod Zod schema client related issue label Jun 30, 2026
@arthurfiorette
arthurfiorette marked this pull request as ready for review June 30, 2026 15:04
@arthurfiorette

Copy link
Copy Markdown
Contributor Author

@melloware ready for review.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/content/docs/guides/zod.mdx`:
- Around line 101-104: The Zod config example in the docs is internally
inconsistent: the mini variant only supports Zod 4, but the inline comment on
the version setting still implies 3, 4, and auto are all valid for this exact
snippet. Update the example around the zod variant/version settings so the
comment and surrounding text match the Mini-specific behavior in the relevant
docs section, using the override.zod example as the anchor.
- Around line 213-216: The type-inference example in the Zod Mini guide is using
a named type import, which is inconsistent with the Mini namespace style used
elsewhere. Update the snippet to use the namespace type import from zod/mini and
reference inference through zod.infer in the Pet type alias, keeping the example
aligned with the existing Mini import pattern.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 734dea71-dc53-4e3d-8083-967f94cb4c8a

📥 Commits

Reviewing files that changed from the base of the PR and between d2abeaa and 8c202ef.

📒 Files selected for processing (13)
  • docs/content/docs/guides/zod.mdx
  • docs/content/docs/reference/configuration/output.mdx
  • packages/angular/src/http-client.test.ts
  • packages/angular/src/http-resource.test.ts
  • packages/core/src/test-utils/context.ts
  • packages/mock/src/faker/getters/combine.test.ts
  • packages/orval/src/reusable-schemas.test.ts
  • packages/orval/src/reusable-schemas.ts
  • packages/orval/src/write-zod-specs.test.ts
  • packages/orval/src/write-zod-specs.ts
  • packages/solid-start/src/index.test.ts
  • packages/zod/src/index.ts
  • packages/zod/src/zod.test.ts
✅ Files skipped from review due to trivial changes (2)
  • packages/mock/src/faker/getters/combine.test.ts
  • docs/content/docs/reference/configuration/output.mdx
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/orval/src/write-zod-specs.test.ts
  • packages/zod/src/index.ts
  • packages/orval/src/write-zod-specs.ts

Comment thread docs/content/docs/guides/zod.mdx
Comment thread docs/content/docs/guides/zod.mdx
@melloware melloware added this to the 8.20.0 milestone Jun 30, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

@orval/angular

bun add https://pkg.pr.new/@orval/angular@99ddae0

@orval/axios

bun add https://pkg.pr.new/@orval/axios@99ddae0

@orval/core

bun add https://pkg.pr.new/@orval/core@99ddae0

@orval/effect

bun add https://pkg.pr.new/@orval/effect@99ddae0

@orval/fetch

bun add https://pkg.pr.new/@orval/fetch@99ddae0

@orval/hono

bun add https://pkg.pr.new/@orval/hono@99ddae0

@orval/mcp

bun add https://pkg.pr.new/@orval/mcp@99ddae0

@orval/mock

bun add https://pkg.pr.new/@orval/mock@99ddae0

orval

bun add https://pkg.pr.new/orval@99ddae0

@orval/query

bun add https://pkg.pr.new/@orval/query@99ddae0

@orval/solid-start

bun add https://pkg.pr.new/@orval/solid-start@99ddae0

@orval/swr

bun add https://pkg.pr.new/@orval/swr@99ddae0

@orval/zod

bun add https://pkg.pr.new/@orval/zod@99ddae0

commit: 99ddae0

@anymaniax anymaniax left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems really great for me maybe @melloware or @soartec-lab want to check too?

@melloware

Copy link
Copy Markdown
Collaborator

agreed this is great. He is fixing the Preview push so he can test it in his actual production code as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

zod Zod schema client related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants