P
Prisma•5d ago
Risatoga

Typing Json fields

I am working with prisma and when I have to save an image, I save them as json value, like this: " model Tattoo { //... image Json //... } " then the image field is an object with fields like "publicUrl", "fileName", "fileSize" The problem is that with this approach, prisma offers no validation or types for the image field: I can add images that do not fit in the expected shape. What other approach could I use to make my image field more "type safe"? I am now using Prisma Json types as recommended in: https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/working-with-json-fields but that is "clashing" with zod-prisma-types (https://www.npmjs.com/package/zod-prisma-types) which is a library I enjoy a lot using... Basically, while both keep doing their work, as zod-prisma-types is based on the prisma schema file, and there the json file is not type, the types and schemas generated by it are too wide (aka: not applying the types generated by json type generator) Is there any fix?
Working with Json fields (Concepts) | Prisma Documentation
How to read, write, and filter by Json fields.
npm
zod-prisma-types
Generates zod schemas from Prisma models with advanced validation. Latest version: 3.1.8, last published: 5 months ago. Start using zod-prisma-types in your project by running npm i zod-prisma-types. There are 6 other projects in the npm registry using zod-prisma-types.
3 Replies
Nurul
Nurul•5d ago
Hey @Risatoga 👋 Did you had a chance to look at this npm package? https://www.npmjs.com/package/prisma-json-types-generator This should resolved the issue for you.
npm
prisma-json-types-generator
Changes JsonValues to your custom typescript type. Latest version: 3.1.1, last published: 18 days ago. Start using prisma-json-types-generator in your project by running npm i prisma-json-types-generator. There are 7 other projects in the npm registry using prisma-json-types-generator.
Risatoga
Risatoga•4d ago
yep, is the one I said I am using, but is "clashing" with the zod-prisma-types one (which I really like!) I was wondering if there is a better way other than using two generators for this...?
Nurul
Nurul•2d ago
Can you elaborate on how it is clashing? Have you seen this related GitHub Issue? There are a few suggested solutions in it https://github.com/prisma/prisma/issues/3219
GitHub
Define type of content of Json field · Issue #3219 · prisma/prisma
Problem Right now if you have the following schema with Json field: model User { id Int @default(autoincrement()) @id name String? extendedProfile Json } You'll end up with a problem that you d...
Want results from more Discord servers?
Add your server