I have several `Json` fields defined in my models....
# orm-help
r
I have several
Json
fields defined in my models. For example:
Copy code
model Template {
 pages Json @default("[]")
}
An example of a page is:
Copy code
{
 title: string; // e.g. 'blah'
}
Because we cannot add specific types to
Json
fields for postgres yet, when I start looping through
pages
all of the keys throw a typescript error like the image below. It is a
Prisma.JsonValue
instead of a
string
. This forces me to put the values in a variable and cast them as the correct type whenever I want to reference them. As you can expect, that litters the code base with casts and is certainly not what I want. Maybe I’m missing something… how are folks dealing with this?
c
https://github.com/colinhacks/zod I used to worry about this (and get very nitpicky about reducing casts and validation) but end of the day correctness and type safety helps with the end goal of shipping faster more than whether the code feels succinct imo
👍 1
r
Thanks @Casey Chow. So you use zod along with
@prisma/client
types?
c
Yep, usually what I do is put a schema in a special file for the entity and it’s relatively greppable when all your usages of the field are immediately parsed out. (I know Prisma also has plans to eventually support custom fields, but I’m not really holding my breath and personally prefer the obviousness of “if it looks like this in the DB I know how it looks like in the generated types”.)