drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: drizzle-zod not checking for varchar length

Open TiltedToast opened this issue 1 year ago • 3 comments

What version of drizzle-orm are you using?

0.25.4

What version of drizzle-kit are you using?

0.17.6

Describe the Bug

drizzle-zod (0.4.1) does not check for things like varchar length (and maybe some others i'm not sure)

Given the schemas

import { z } from "zod";
import { createInsertSchema } from "drizzle-zod";

export const redditPosts = mysqlTable(
    "reddit_posts",
    {
        id: int("id").autoincrement().primaryKey(),
        subreddit: varchar("subreddit", { length: 50 }).notNull(),
        title: varchar("title", { length: 255 }).notNull(),
        url: varchar("url", { length: 255 }).notNull(),
        over_18: boolean("over_18").notNull(),
        permalink: varchar("permalink", { length: 255 }).notNull(),
    },
    (table) => {
        return {
            subreddit: index("subreddit").on(table.subreddit),
        };
    }
);

export const InsertRedditPostSchema = createInsertSchema(redditPosts);

Now when you take an object such as

const post = {
   id: 1,
   subreddit: "A".repeat(60),  // VARCHAR(50)
   title:     "A".repeat(300), // VARCHAR(255)
   url:       "A".repeat(300), // VARCHAR(255)
   permalink: "A".repeat(300), // VARCHAR(255)
   over_18:   false,           // BOOLEAN
};

and try to parse it with the above generated zod schema

InsertRedditPostSchema.parse(post);

it doesn't complain about the length of any fields

TiltedToast avatar May 13 '23 12:05 TiltedToast

@TiltedToast this should now be fixed in drizzle-zod@beta.

dankochetov avatar May 29 '23 19:05 dankochetov

@dankochetov Tested on beta and 0.4.2 still have the issue. Also talking about zod, are you sure length is the right one to use there? Feels like max instead of length would be more appropriate 🤔

At least as personal experience it is more common to see fields that can take up to N in length than a field that contain the exact length.

marlomgirardi avatar May 31 '23 21:05 marlomgirardi

The thing that confuses me the most is that it doesn't even seem to care about the length at all. Zod's length function makes it so the length of whatever string you're passing it has to be exactly what you specify.

This should cause it to fail pretty much every time, but it doesn't so the check must be failing for some reason I guess. Breaking up the Char and Varchar columns to use length and max respectively would probably make a lot of sense, since that's how they work. Might have to open up a PR for it

TiltedToast avatar Jun 01 '23 23:06 TiltedToast