PrismaP
Prisma16mo ago
4 replies
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?
How to read, write, and filter by Json fields.
Working with Json fields (Concepts) | Prisma Documentation
npm
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.
zod-prisma-types
Was this page helpful?