Skip to content

Support zod codecs (https://github.com/trpc/trpc/issues/6978) - #1

Open
lukeramsden wants to merge 3 commits into
mainfrom
claude/update-zod-codecs-01AM4iCMvrqXxUf3ZAwxR9SR
Open

lukeramsden wants to merge 3 commits into
mainfrom
claude/update-zod-codecs-01AM4iCMvrqXxUf3ZAwxR9SR

Conversation

@lukeramsden

Copy link
Copy Markdown
Owner

No description provided.

This change enables proper type inference for output functions where
the input type differs from the output type, such as Zod codecs that
use encode/decode transformations.

Changes:
- Add TInput type parameter to ParseFn (defaults to unknown for backward compat)
- Update ParserCustomValidatorEsque to support explicit input types
- Update inferParser to distinguish between unknown input (validators)
  and typed input (transforms/codecs)
- Update createOutputMiddleware to accept typed ParseFn

When using a typed output function like:
  const encoder = (value: bigint): string => value.toString();
  procedure.output(encoder).query(() => 123n);

The resolver is now correctly typed to return bigint (encoder input),
and the client receives string (encoder output).

This enables type-safe usage of Zod v4.1 codecs without type coercions.
Add a skipped test that documents how tRPC would work with actual
Zod codecs once the project upgrades to Zod v4.1+. The test includes
commented code showing the intended usage pattern with z.codec().

Codecs were introduced in Zod 4.1.0: https://zod.dev/codecs
Add native support for Zod v4.1+ codecs with a new `.outputEncoder()` method
on the procedure builder. This method uses the schema's encode() function
instead of parse(), properly handling the reversed type flow for output encoding.

Changes:
- Add ParserWithEncode type for schemas with encode capability
- Add inferEncoder type that swaps in/out for encoding direction
- Add getEncodeFn() to extract encode function from codec schemas
- Add hasEncodeFn() runtime check for codec detection
- Add outputEncoder() method to ProcedureBuilder

Usage with Zod v4.1+ codecs:
```typescript
const stringToBigInt = z.codec(z.string(), z.bigint(), {
  decode: (s) => BigInt(s),
  encode: (n) => n.toString(),
});

procedure
  .outputEncoder(z.object({ value: stringToBigInt }))
  .query(() => ({ value: 123n })); // Returns bigint, client receives string
```

The key difference from .output():
- output() uses parse(): _input (wire) -> _output (runtime)
- outputEncoder() uses encode(): _output (runtime) -> _input (wire)
middlewares: [createOutputMiddleware(parser)],
});
},
outputEncoder(encoder: Encoder) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I don't know if there is a better way to do this or not - I don't think codecs sit within Standard Schema correctly?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

standard-schema/standard-schema#14

Seems like Zod has gone far beyond Standard Schema already

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.

2 participants