drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.57k stars 490 forks source link

[BUG]: drizzle-zod generates ZodString type for numeric field #2353

Open cellulosa opened 1 month ago

cellulosa commented 1 month ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.2

Describe the Bug

I defined a numeric field:

import { pgTable, text, timestamp, numeric } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';

export const user = pgTable('user', {
    id: text('id').primaryKey(),
    githubId: numeric('github_id').unique()
});

export const insertUserSchema = createInsertSchema(user);
export const selectUserSchema = createSelectSchema(user);

The types generated with createInsertSchema / createSelectSchema result in:

const insertUserSchema: ZodObject<{
    id: ZodString;
    githubId: ZodOptional<ZodNullable<ZodString>>;
},

therefore the parsing fails:

insertUserSchema.parse({
    id: generateId(15),
    githubId: 0123456789
})

Expected behavior

numeric() should be converted to ZodNumber

Environment & setup

drizzle-zod 0.5.1

cellulosa commented 1 month ago

for the moment this issue can be circumnavigated by overriding insertUserSchema:

export const insertUserSchema = createInsertSchema(user, {
    githubId: z.number()
});