You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, I have some entities that use the Decimal type
For example:
...
column_name Decimal? @db.Decimal(18, 4)
...
When I generate zod schemas for entities like these, I would simply get
...
column_name: z.number().nullish(),
...
Now, when I query data from this table using Prisma, the columns that are of Decimal type are objects under the hood, and therefore when I try to validate this data using Zod, I get an error, something along the line of invalid_type expected number (according to schema) received object (when queried with Prisma)
If instead I would do:
...
column_name: z.object().nullish(),
...
This would work.
Is there any workaround to this? I have a lot of schemas and do not want to change each of them. Would it be possible to make it so that, upon schema generation, for decimal types, to not use z.number().nullish(), and instead use some schema to validate the Decimal object correctly?
Also, in every schema file, on the top I have:
// Helper schema for Decimal fields
z.instanceof(Decimal)
.or(z.string())
.or(z.number())
.refine(value => {
try {
return new Decimal(value);
} catch (error) {
return false;
}
})
.transform(value => new Decimal(value));
What exactly does this do? Is it supposed to solve this problem? Why is it not working?
See this issue
The text was updated successfully, but these errors were encountered:
ElhanM
changed the title
Now working with Decimal in Prisma model - wrong schema
Wrong Zod schema when working with Decimal in Prisma model
May 22, 2024
I think the Helper schema for Decimal fields was supposed to be a fix to this problem that got dropped somewhere along the way.
Someone tried to fix it here
In
schema.prisma
I have:
So useDecimalJs is set to true
Now, I have some entities that use the Decimal type
For example:
When I generate zod schemas for entities like these, I would simply get
Now, when I query data from this table using Prisma, the columns that are of Decimal type are objects under the hood, and therefore when I try to validate this data using Zod, I get an error, something along the line of
invalid_type expected number (according to schema) received object (when queried with Prisma)
If instead I would do:
This would work.
Is there any workaround to this? I have a lot of schemas and do not want to change each of them. Would it be possible to make it so that, upon schema generation, for decimal types, to not use
z.number().nullish()
, and instead use some schema to validate the Decimal object correctly?Also, in every schema file, on the top I have:
What exactly does this do? Is it supposed to solve this problem? Why is it not working?
See this issue
The text was updated successfully, but these errors were encountered: