Skip to content

Conversation

@KATT
Copy link
Member

@KATT KATT commented Jun 24, 2025

Closes #6831

🎯 Changes

Demo of zod branded types

Summary by CodeRabbit

  • Tests
    • Added a new test to verify correct handling and inference of branded Zod v4 types in TRPC router procedures.

@KATT KATT requested a review from a team as a code owner June 24, 2025 10:04
@railway-app
Copy link

railway-app bot commented Jun 24, 2025

🚅 Deployed to the trpc-pr-6833 environment in trpc-sse-and-websockets

Service Status Web Updated (UTC)
next-sse-chat ◻️ Removed (View Logs) Web Jun 24, 2025 at 2:25 pm
next-prisma-websockets-starter ◻️ Removed (View Logs) Jun 24, 2025 at 2:25 pm

@railway-app railway-app bot temporarily deployed to trpc-pr-6833 June 24, 2025 10:04 Destroyed
@railway-app railway-app bot temporarily deployed to trpc-pr-6833 June 24, 2025 10:04 Destroyed
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 24, 2025

Walkthrough

A new test case was added to validators.test.ts to verify that Zod v4 branded types are correctly inferred and preserved when used as TRPC router inputs and outputs. The test checks both runtime and type-level preservation of branded types. Imports were updated to include necessary TRPC type inference utilities.

Changes

File(s) Change Summary
packages/tests/server/validators.test.ts Added test for Zod v4 branded types in TRPC router; expanded imports to include type inference utils

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant TRPC_Router
    participant Zod4_Schema

    Test->>Zod4_Schema: Define branded schema (AccountId)
    Test->>TRPC_Router: Initialize router with procedure using AccountId
    Test->>TRPC_Router: Call procedure with branded input
    TRPC_Router->>Test: Return input (runtime check)
    Test->>Test: Assert input/output types using inferRouterInputs/Outputs
Loading

Assessment against linked issues

Objective Addressed Explanation
Verify TRPC correctly passes down Zod 4.0 branded types in input/output (#6831)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Poem

A test for branded types, oh what a feat!
TRPC and Zod4 now properly meet.
AccountId’s brand, preserved in the flow,
Type inference checked, so bugs won’t grow.
With every test written, our code’s more robust,
This rabbit’s quite happy—hopping in trust! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch issues/6831-branded-zod

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel
Copy link

vercel bot commented Jun 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-prisma-starter ✅ Ready (Inspect) Visit Preview Jun 24, 2025 10:08am
og-image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2025 10:08am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2025 10:08am

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/tests/server/validators.test.ts (1)

922-950: Well-structured test for branded types, but consider adding runtime validation.

The test correctly verifies that Zod v4 branded types are preserved through TRPC's type inference system. The type-level assertions using expectTypeOf are appropriate and comprehensive.

However, the test only includes compile-time type checking without runtime validation. Consider adding runtime assertions to ensure the branded types work correctly at runtime as well.

Consider adding runtime validation to make the test more comprehensive:

 test('zod4 branded types', () => {
   const t = initTRPC.create();

   const AccountId = zod4.cuid2().brand<'EmailAccount'>();
   type Types = NonNullable<(typeof AccountId)['~standard']['types']>;
   const router = t.router({
     num: t.procedure
       .input(
         zod4.object({
           accountId: AccountId,
         }),
       )
       .query((opts) => {
         expectTypeOf(opts.input.accountId).toEqualTypeOf<Types['output']>();
         return opts.input;
       }),
   });

   type RouterInput = inferRouterInputs<typeof router>;
   type RouterOutput = inferRouterOutputs<typeof router>;

   type AccountIdInput = RouterInput['num']['accountId'];
   //     ^?
   type AccountIdOutput = RouterOutput['num']['accountId'];
   //    ^?

   expectTypeOf<AccountIdInput>().toEqualTypeOf<Types['input']>();
   expectTypeOf<AccountIdOutput>().toEqualTypeOf<Types['output']>();
+
+  // Runtime validation to ensure branded types work correctly
+  const caller = router.createCaller({});
+  const validCuid = 'ckpfn8ueu000001l60fv4j3ka'; // valid CUID2
+  expect(() => caller.num({ accountId: validCuid })).not.toThrow();
+  expect(() => caller.num({ accountId: 'invalid-cuid' })).toThrow();
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e548cf and 0837014.

📒 Files selected for processing (1)
  • packages/tests/server/validators.test.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: E2E-tests (vercel-edge-runtime)
  • GitHub Check: E2E-tests (fastify-server)
  • GitHub Check: E2E-tests (next-prisma-todomvc)
  • GitHub Check: E2E-tests (express-minimal)
  • GitHub Check: E2E-tests (soa)
  • GitHub Check: E2E-tests (express-server)
  • GitHub Check: E2E-tests (next-formdata)
  • GitHub Check: E2E-tests (.experimental/next-app-dir)
  • GitHub Check: E2E-tests (.test/diagnostics-big-router)
  • GitHub Check: E2E-tests (cloudflare-workers)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
  • GitHub Check: E2E-tests (.test/internal-types-export)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
  • GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
  • GitHub Check: Release using pkg.pr.new
  • GitHub Check: E2E-tests (Deno) (deno-deploy)
  • GitHub Check: test
  • GitHub Check: Lint and auto-fix
🔇 Additional comments (1)
packages/tests/server/validators.test.ts (1)

5-10: LGTM! Import changes support the new test functionality.

The addition of inferRouterInputs and inferRouterOutputs type utilities is correctly implemented and necessary for the new branded types test case.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 24, 2025

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6833

@trpc/next

npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6833

@trpc/react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6833

@trpc/server

npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6833

@trpc/tanstack-react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/tanstack-react-query@6833

@trpc/upgrade

npm i https://pkg.pr.new/trpc/trpc/@trpc/upgrade@6833

commit: 0837014

@KATT KATT changed the title Issues/6831 branded zod chore: add test for zod branded types Jun 24, 2025
@KATT KATT merged commit 5b325d7 into main Jun 24, 2025
47 checks passed
@KATT KATT deleted the issues/6831-branded-zod branch June 24, 2025 14:25
@github-actions
Copy link
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: trpc does not propertly pass down zod 4.0 branded types

3 participants