Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

z.discriminatedUnion nested in procedure output makes trpc-panel render as blank page without any warnings/errors #107

Open
DJanocha opened this issue Aug 26, 2024 · 1 comment

Comments

@DJanocha
Copy link

First of all: library is awesome and helps a lot during development. Thank you for you work!

Unfortunately I cannot show a code snippet due to security reasons but more or less the problem looks like described below.

const unionASchema = z.object({
  type: z.literal("a"),
  other: z.string(),
  other2: z.number(),
})
const unionBSchema = z.object({
  type: z.literal("b"),
  other3: z.string(),
})

export const unionSchema = z.discriminatedUnion("type", [unionASchema, unionBSchema])
const userSchema = z.object({
  name: z.string(),
  age: z.number(),
  custom: unionSchema,
})

And then in some procedure, 'output' is declared using userSchema schema.

This resulted in blank page when navigated to '/api/trpc-panel' which rendered this:

import { type Request, type Response } from "express";
import { renderTrpcPanel } from "trpc-panel";

import { appRouter } from "../routers/index";

export const trpcPanelMiddleware = (req: Request, res: Response) => {
  return res.send(
    renderTrpcPanel(appRouter, {
      url: "/api/trpc",
      transformer: "superjson",
    }),
  );
};

I found out that when I change z.discriminatedUnion to z.union, everything starts to work again. But AFAIK, type narrowing works worse in z.union compared to z.discriminatedUnion.

I understand that that could be hard to fix/address, but I just wanted to leave that for others that could found themselves in similar problem.

@david8z
Copy link

david8z commented Dec 2, 2024

Sam Issue here.
Did you found any fix?

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

No branches or pull requests

4 participants
@david8z @DJanocha and others