tRPCttRPC
Powered by
shikishikichangchangS
tRPC•17mo ago•
2 replies
shikishikichangchang

Override output schema for certain mutations

Is it possible to overrider output schema's type for certain mutations?

Currently I have a base procedure that has an outputschema as such:

import { z } from "zod";

const successSchema = z.object({
    status: z.literal("success"),
    data: z.any(),
});

const errorSchema = z.object({
    status: z.literal("error"),
    errorMessage: z.string(),
});

export const outputSchema = z.discriminatedUnion("status", [successSchema, errorSchema]);
export type Output = z.infer<typeof outputSchema>;
import { z } from "zod";

const successSchema = z.object({
    status: z.literal("success"),
    data: z.any(),
});

const errorSchema = z.object({
    status: z.literal("error"),
    errorMessage: z.string(),
});

export const outputSchema = z.discriminatedUnion("status", [successSchema, errorSchema]);
export type Output = z.infer<typeof outputSchema>;


As you can see, data is z.any(). For most mutations I don't need to return the data so that's not a problem. However, I want to return the data with the correct type for some.

The output must have either z.literal("success") or z.literal("error").

I tried to add an extra '.output' but it seems like trpc only allows for one (no chaining to override).
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

sanitizing output based on output schema
y_nkYy_nk / ❓-help
7mo ago
trpc mutations for searching data
PotatisMannenPPotatisMannen / ❓-help
3y ago
Custom data transformer and input/output schema
Titouan Créac'hTTitouan Créac'h / ❓-help
2y ago
Typescript helper for router output
Chris JaydenCChris Jayden / ❓-help
3y ago