image validation using zod

i wrote this in the tRPC router but it gave this error
createComic: protectedProcedure
.input(
z.object({
title: z.string(),
description: z.string(),
coverImage: z
.any()
.refine((files) => files?.length == 1, "Image is required.")
.refine(
(files) => files?.[0]?.size <= MAX_FILE_SIZE,
`Max file size is 5MB.`
)
.refine(
(files) => ACCEPTED_IMAGE_TYPES.includes(files?.[0]?.type),
".jpg, .jpeg, .png and .webp files are accepted."
),
})
)
.mutation(async ({ input, ctx }) => {
const prisma = ctx.prisma;

//upload to supabase
const { data, error } = await ctx.supabase.storage
.from("comic-cover")
.upload(`comic-${input.title}.png`, input.coverImage, {
cacheControl: "3600",
upsert: false,
});
if (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: error.message,
});
}
//get the url
const { data: url } = ctx.supabase.storage
.from("comic-cover")
.getPublicUrl(data.path);

const comic = await prisma.comic.create({
data: {
title: input.title,
description: input.description,
poster: url.publicUrl,
},
});
}),
createComic: protectedProcedure
.input(
z.object({
title: z.string(),
description: z.string(),
coverImage: z
.any()
.refine((files) => files?.length == 1, "Image is required.")
.refine(
(files) => files?.[0]?.size <= MAX_FILE_SIZE,
`Max file size is 5MB.`
)
.refine(
(files) => ACCEPTED_IMAGE_TYPES.includes(files?.[0]?.type),
".jpg, .jpeg, .png and .webp files are accepted."
),
})
)
.mutation(async ({ input, ctx }) => {
const prisma = ctx.prisma;

//upload to supabase
const { data, error } = await ctx.supabase.storage
.from("comic-cover")
.upload(`comic-${input.title}.png`, input.coverImage, {
cacheControl: "3600",
upsert: false,
});
if (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: error.message,
});
}
//get the url
const { data: url } = ctx.supabase.storage
.from("comic-cover")
.getPublicUrl(data.path);

const comic = await prisma.comic.create({
data: {
title: input.title,
description: input.description,
poster: url.publicUrl,
},
});
}),
2 Replies
Samathingamajig
did you upload an image that was either >= 5MB, or wasn't jpg/jpeg/png/webp?
arete
areteOP2y ago
never mind those, i can already upload it to supabase but the problem is the image is kinda broken theres no error on the image i think should i put supabase on my ctx router?
Want results from more Discord servers?
Add your server