tRPCttRPC
Powered by
BlitzB
tRPC•3y 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();
}
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
input
is unknown and gives rise to a bunch of type errors
Apologies in advance for the newbie question
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Request context inside middleware?
HuzefHHuzef / ❓-help
4y ago
TRPC Middleware w/ Input
LiamLLiam / ❓-help
3y ago
Modifying input in a middleware
Gobot1234GGobot1234 / ❓-help
13mo ago