Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
9 replies
janglad

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 Client, Migrate, Introspection error message reference
Prisma error message reference (Reference)
Was this page helpful?