I started using Prisma a few weeks ago and absolut...
# orm-help
t
I started using Prisma a few weeks ago and absolutely love it. One thing I can't seems to find is how to do schema validations. For example, if I have a field that is an int between 1 and 10 characters, is there a way to attach this to the Prisma Schema? All the articles I have read say you need to use something like Joi or Zod. Any ideas?
a
Exactly what those article said. Prisma is an ORM not a schema validation library. I think they partly support CHECK constraints at the database level but good luck with that. The best thing you can do is just use Joi, Zod or class validator library
I solved it by creating a class that implements the prisma generated types and then added decorators from class-validator library
t
Thanks @Adrian. I am currently using TypeORM for a project because I can use classes for my models, but I think your suggestion above is not a bad idea.
a
I am using TypeORM as well as class-validator and class-transformer in NestJS. We will migrate to Prisma in the following months in the company I work at and what I told you was the best approach I found for schema validation. Maybe it is not the best but didn't want to spend time looking into other libraries
The problem I found is that TypeScript as no way to prevent a class from having more properties than the interface it implements, so if I were to change the name of a field in prisma or delete a field then the validation class would not throw a TS error to remember the developers on the team from forgetting to update the validation schema
Anyways peer-review to the rescue here
t
Ah. Good point. Any particular reason you are switching off TypeORM?
a
I find it a pain in the ass to work with it. TS support is better in Prisma. Also TypeORM is less mantained.