BlitzB
tRPC3y ago
3 replies
Blitz

Validating input inside middleware declaration

const enforceUserIsCreatorOfEvent = t.middleware(({ ctx, next, input }) => {
  if (!input.eventId || !ctx.session?.user) {
    throw new TRPCError({ code: "BAD_REQUEST" });
  }
  
  const { eventId } = input;
  const { user } = ctx.session;

  if (!user) {
    throw new TRPCError({ code: "UNAUTHORIZED" });
  }

  const event = await ctx.prisma.event.findUnique({
    where: {
      id: eventId,
    },
  });

  if (!event) {
    throw new TRPCError({ code: "NOT_FOUND" });
  }

  if (event.creatorId !== user.id) {
    throw new TRPCError({ code: "UNAUTHORIZED" });
  }

  return next();
}


But type of input is unknown and gives rise to a bunch of type errors
Apologies in advance for the newbie question
Was this page helpful?