There is an open feature request for Prisma to support runtime model validation directly at the Schema level. Alternatively, you can leverage the Client Extensions to perform validation. There is an example in this blog post that shows how to perform custom runtime validation.
Answer from Raphael Etim on Stack Overflownext.js - "Single source of truth" schema/validation class for server (NestJs/GraphQL/TypeGraphQL/Prisma) and client (NextJs) - Stack Overflow
Which source of truth in a TS/Zod/Prisma project?
Exposing Prisma's internal validation functions
validate form
» npm install zod-prisma-types
Note: I’m one of the authors of Remult
With Remult you have one TypeScript class (per entity) which serves as a single source of truth for the API endpoints, API client and ORM. It can also run TypeScript validation code, defined in entity decorators, in both the frontend (e.g. before mutation requests are sent) and the backend.
Only caveat is that while the current version of Remult can be used to expose a GraphQL endpoint, the current API client only supports RESTful. Other than that I think it can fit nicely in your stack.
A "single source of truth" for both front and serverside data validation is also something I have been, for a long time, searching for.
I recently learned about "community generators" for Prisma, which allow you to generate schemas for joi, (also yup, class validator, ...) from your Prisma model.
In theory, you should be able to use these for validation of your front end forms. Not sure yet how feature complete they are though.
https://www.prisma.io/docs/concepts/components/prisma-schema/generators#community-generators
prisma-json-schema-generator: @grimen prisma-joi-generator: Generate full Joi schemas from your Prisma schema.
Source: https://github.com/prisma/prisma/issues/3528#issuecomment-1133879401
I have a Typescript project and want to use form validation with Zod on the front end. I’m also using Prisma ORM. The issue is I’m struggling to get one source of truth for the resource being created/updated. How do you keep those in sync? At the moment, I have 3 separate schemas. What’s the best practice to define your schema once and keep TS, DB and frontend validation in sync?