Prisma + Zod, where to validate uniqueness
Say I have a Prisma schema with multiple unique fields (for example an ID and a URL), where do I validate the uniqueness of these fields on the server? I could use an async ZOD refinement, which makes it easy to provide a custom error message etc to the user but this is more costly DB wise. I could also just let Prisma throw, but formatting a custom error message there is a bit of a pain. It's possible to check the type of the error and implement custom logic based on the code, but the field on which the error is thrown is passed through
meta.target
which doesn't seem to be properly typed. This results in some quite ugly code checking the code
of the error first, checking if there is a meta
, a target
and checking if that is an array.
https://www.prisma.io/docs/reference/api-reference/error-reference#prismaclientknownrequesterror
How do you guys handle this?Prisma
Prisma error message reference (Reference)
Prisma Client, Migrate, Introspection error message reference
3 Replies
When this happens, you DB will throw the error and prisma will catch it... that's why you need to wrap all prisma calls in try{}catch{}
I forgot how the error object looks exactly but I know that each such error has a specific code in the DB
In my case, with PostgreSQL, when you add a duplicate id it says error P2001
Yup this is true and I'm doing this but I'm facing some challenges formatting that into a nice message for the user @juliecezar
Specifically getting the name of the field(s) that caused the error, cause it could be multiple if a model has multiple fields that should be unique
I've created a utility function validateUniqueConstraint to handle unique constraint errors from Prisma. It takes in a field name, error message, and a function that triggers a Prisma operation. If a unique constraint error occurs on the specified field, it throws a user-friendly TRPCError with the provided error message. Then I handle this error using enum on the frontend.
frontend: