Skip to content

fix: Zod 4 compatibility for prefault, transform pipes, and refined schema omit#155

Merged
mcampa merged 1 commit into
mcampa:masterfrom
MendyLanda:fix/zod4-compatibility
Mar 19, 2026
Merged

fix: Zod 4 compatibility for prefault, transform pipes, and refined schema omit#155
mcampa merged 1 commit into
mcampa:masterfrom
MendyLanda:fix/zod4-compatibility

Conversation

@MendyLanda
Copy link
Copy Markdown

Summary

Fixes compatibility issues when using trpc-to-openapi with Zod 4:

  • prefault type support: Zod 4 introduces a prefault type (similar to default). Added handling in unwrapZodType to unwrap it via .unwrap(), matching the existing default behavior.
  • .transform() pipe handling: In Zod 4, .transform().superRefine() creates a pipe where def.out is an opaque transform type rather than a usable schema. When this is detected, the unwrapper now follows def.in (the input schema shape) instead.
  • Refined schema .omit() fallback: Zod 4 throws when calling .omit() on object schemas that have refinements. Wrapped the .omit() call in getRequestBodyObject with a try/catch that falls back to manually filtering path parameter keys from schema.shape.

Fixes #153

Test plan

  • Verify unwrapZodType correctly unwraps z.prefault() schemas
  • Verify unwrapZodType follows def.in for pipes where def.out is a transform
  • Verify getRequestBodyObject works with refined object schemas that have path parameters
  • Verify existing behavior is unchanged for non-Zod-4 usage patterns

…ed schema omit

- Handle Zod 4's `prefault` type in `unwrapZodType` (same unwrap logic as `default`)
- In `pipe` handling, check if `def.out` is a `transform` type and follow `def.in` instead, since `.transform().superRefine()` in Zod 4 creates a pipe where `def.out` is an opaque transform
- Wrap `.omit()` in try/catch in `getRequestBodyObject` to handle refined schemas that throw in Zod 4, falling back to manual shape filtering

Fixes mcampa#153
@mcgilly17
Copy link
Copy Markdown

Would love to see this merged if possible.

@masterjanic
Copy link
Copy Markdown

@mcampa Any chance to get this merged?

@MendyLanda
Copy link
Copy Markdown
Author

To everyone waiting for the merge, just give this pr link to your agent of choice and ask it to patch it for you.

@mcampa
Copy link
Copy Markdown
Owner

mcampa commented Mar 19, 2026

Hey, Sorry I haven't have time to look at PRs here. Going to merge it and publish a release

@masterjanic
Copy link
Copy Markdown

trpc-to-openapi@3.1.0.patch

I will leave this here for other people. If you are using bun just create a folder patches at the root of your project with the file and append this to package.json

  "patchedDependencies": {
    "trpc-to-openapi@3.1.0": "patches/trpc-to-openapi@3.1.0.patch"
  }

@mcampa mcampa merged commit 8bb1e46 into mcampa:master Mar 19, 2026
3 checks passed
@mcampa
Copy link
Copy Markdown
Owner

mcampa commented Mar 19, 2026

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.

Zod v4 issue with refined schemas

4 participants