DangerZone
DangerZone7mo ago

Validating inputs and outputs only via typescript

Hey everyone, I am trying to create an appRouter with ts-morph but I am having trouble with passing the zod schema/object instance to it (since I need to pass the original implementation, not the instance). I was thinking about a way to work around this and was wondering if I could somehow just pass the zod typescript type in the inputs and outputs - keep in mind that my usecase only calls for the type definitions to work, not the actual implementation.
import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();
const publicProcedure = t.procedure;

const randomSchema = z.object({
name: z.string(),
});

type RandomType = z.infer<typeof randomSchema>;

const appRouter = t.router({
UserRouter: {
hello: publicProcedure.input<RandomType>().query((opts) => {
const name = opts.input.name;
return {
greeting: `Hello ${opts.input.name}`,
};
}),
},
});
export type AppRouter = typeof appRouter;
import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();
const publicProcedure = t.procedure;

const randomSchema = z.object({
name: z.string(),
});

type RandomType = z.infer<typeof randomSchema>;

const appRouter = t.router({
UserRouter: {
hello: publicProcedure.input<RandomType>().query((opts) => {
const name = opts.input.name;
return {
greeting: `Hello ${opts.input.name}`,
};
}),
},
});
export type AppRouter = typeof appRouter;
here you can see I am trying to pass on;y the RandomType, but ofc I don't really know what I am doing 😄
0 Replies
No replies yetBe the first to reply to this messageJoin