Check if unique key exists before create

I'm using react-hook-form with zod validation, and I want to check if a unique key exists on the onBlur field event.

I can't put it inside Zod validation nor can I rely on the server to respond with this error after submission, because planet-scale never knows what column caused the error.

My workaround was to create a trpc route to check if the key exists, call a it's refetch every onBlur event and conditionally use setError to invalidate the field. But this approach is cumbersome and doesn't seem like a good solution.

What could I do instead?
Was this page helpful?