elie
elie13mo ago

Error when converting string from router.query to prisma DB enum

Hey yall, I'm working on a Next.js application using tRPC and Prisma, and I've encountered an invalid_enum_value error when trying to filter service providers by category in my listing.getAll procedure. Here's the error message I'm receiving:
tRPC failed on listing.getAll: [
{
"received": "Primary Care Consultations",
"code": "invalid_enum_value",
"options": [
"DENTAL",
"SKINCARE",
"CARDIOLOGY",
"PCC"
],
"path": [
"category"
],
"message": "Invalid enum value. Expected 'DENTAL' | 'SKINCARE' | 'CARDIOLOGY' | 'PCC', received 'Primary Care Consultations'"
}
]
tRPC failed on listing.getAll: [
{
"received": "Primary Care Consultations",
"code": "invalid_enum_value",
"options": [
"DENTAL",
"SKINCARE",
"CARDIOLOGY",
"PCC"
],
"path": [
"category"
],
"message": "Invalid enum value. Expected 'DENTAL' | 'SKINCARE' | 'CARDIOLOGY' | 'PCC', received 'Primary Care Consultations'"
}
]
In my routers/listings.ts file, I have a getAll procedure that is supposed to validate the user input and map it to the expected enum using a getCategoryEnum function. Despite this, I'm still getting the above error. Here's the relevant part of my listings.ts:
// listings.ts

getAll: publicProcedure
.input(getServiceProviderSchema)
.query(async ({ ctx, input }) => {
// ...other code...
if (input?.category) {
query.category = getCategoryEnum(input.category);
}
// ...other code...
}),
// listings.ts

getAll: publicProcedure
.input(getServiceProviderSchema)
.query(async ({ ctx, input }) => {
// ...other code...
if (input?.category) {
query.category = getCategoryEnum(input.category);
}
// ...other code...
}),
And here's the getCategoryEnum function defined in validation/listing.ts:
// validation/listing.ts

export const getCategoryEnum = (category: string): ServiceCategory | undefined => {
switch (category) {
case 'Primary Care Consultations':
return ServiceCategory.PCC;
case 'Dental Services':
return ServiceCategory.DENTAL;
// ...other cases...
default:
throw new Error(`Invalid category: ${category}`);
}
}
// validation/listing.ts

export const getCategoryEnum = (category: string): ServiceCategory | undefined => {
switch (category) {
case 'Primary Care Consultations':
return ServiceCategory.PCC;
case 'Dental Services':
return ServiceCategory.DENTAL;
// ...other cases...
default:
throw new Error(`Invalid category: ${category}`);
}
}
The ServiceCategory enum is defined in my Prisma schema and includes PCC as one of its values. I'm not sure why the getCategoryEnum function isn't mapping the input 'Primary Care Consultations' to ServiceCategory.PCC as expected. Any insights into what might be causing this error or how to debug it further would be greatly appreciated.
0 Replies
No replies yetBe the first to reply to this messageJoin