Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: drizzle-zod does not respect/count db type range #2737

Closed
valerii15298 opened this issue Aug 1, 2024 · 1 comment
Closed

[BUG]: drizzle-zod does not respect/count db type range #2737

valerii15298 opened this issue Aug 1, 2024 · 1 comment

Comments

@valerii15298
Copy link

valerii15298 commented Aug 1, 2024

What version of drizzle-orm are you using?

0.32.1

What version of drizzle-kit are you using?

0.23.1

Describe the Bug

When I create zod schema for table with integer column type, the created zod schema does not take into account that postgres integer type for example has range only from -2147483648 to +2147483647 https://www.postgresql.org/docs/current/datatype-numeric.html

But the schema does not count it. And I suspect for other db types it does not take their ranges/constraints into account.
And data passed through zod validation will cause error on db side. I suspect a lot of people trust created schema since in docs it is described as type safe.

import * as pgCore from "drizzle-orm/pg-core";

const testTable = pgCore.pgTable("test", {
  num: pgCore.integer("num").notNull(),
});

console.dir(
  createInsertSchema(testTable).safeParse({
    num: 2147483648, // 2**31
  }),
  { depth: null },
);
// output: `{ success: true, data: { num: 2147483648 } }`

Expected behavior

I would expect zod schema to be like this and throw an error in the case I described above:

const schema = z.object({
  num: z
    .number()
    .int()
    .min(-(2 ** 31))
    .max(2 ** 31 - 1),
});

Environment & setup

drizzle-zod 0.5.1
drizzle-orm 0.32.1
drizzle-kit 0.23.1
@valerii15298 valerii15298 added the bug Something isn't working label Aug 1, 2024
@L-Mario564 L-Mario564 added improvement drizzle/zod and removed bug Something isn't working labels Oct 24, 2024
@L-Mario564 L-Mario564 self-assigned this Nov 12, 2024
@AndriiSherman
Copy link
Member

This one was fixed in the latest drizzle-zod@0.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants