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

[BUG]:drizzle-zod produces wrong type

Open SooditK opened this issue 2 years ago • 7 comments

What version of drizzle-orm are you using?

0.28.5

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

I'm using drizzle-orm, drizzle-kit & drizzle-zod in my application. One of my schema looks like following:

export const committee = pgTable("committee", {
  id: text("id").primaryKey().notNull(),
  safety_health: text("safety_health"),
  safety_health_sources: text(
    "safety_health_sources"
  ).array(),
  // other properties
}

The schema works fine when I push, safety_health_sources is created as an array (No issues till this point)

I create zod schema as well, like following:

export const committeeSchema = createSelectSchema(committee);

and when I do:

type Committee = Partial<
  z.infer<typeof committeeSchema>
>;
// the type become something like following
type Committee = {
    id?: string | undefined;
    safety_health?: string | null | undefined;
    safety_health_sources?: string | null | undefined; // <--- The problem is here, it should be an Array<string>
    // other properties
}

Expected behavior

The type should be something like:

type Committee = {
    id?: string | undefined;
    safety_health?: string | null | undefined;
    safety_health_sources?: string[] | null | undefined; // <--- This
    // other properties
}

Environment & setup

NodeJS: v20.5.0 npm: 9.8.0 OS: EndeavourOS Linux x86_64

SooditK avatar Aug 24 '23 16:08 SooditK

I ran into this as well. This fixes it for now

export const selectDataSchema = createSelectSchema(
    responseData,
    {
        text_array: z.array(z.string()),
    },
)

austinm911 avatar Aug 24 '23 19:08 austinm911

The following produces correct types (It's strange):

type Committee = Partial<InferInsertModel<typeof committee>>;

The type it produces:

type Committee = {
    safety_health?: string | null | undefined;
    safety_health_sources?: string[] | null | undefined; // <--- it works, it produces string[]
    // other properties
}

SooditK avatar Aug 31 '23 07:08 SooditK

Ran into this exact problem and have a quick replication example here.

Likely related to https://github.com/drizzle-team/drizzle-orm/issues/1345 as well

CaptainYarb avatar Oct 18 '23 16:10 CaptainYarb

This is showing up in drizzle-typebox too. Array fields in PostgreSQL is also not converting to Array type correct when calling createSelectSchema. Fortunately, drizzle-typebox have options for manual override.

bhuynhdev avatar Dec 20 '23 06:12 bhuynhdev

@dankochetov any updates on this issue 😃

SooditK avatar Dec 20 '23 10:12 SooditK

Just wanted to drop a comment here to say that we ran into this problem in our team as well. In our case we had a column which was an array of an enum, and the solutions above weren't perfect for us as we've have to type out the enum again in two places.

We ended up resorting to a cast:

createSelectSchema(someTable, {
  someField: (_) => _.someField as unknown as ZodArray<typeof _.someField, "many">,
});

This works because curiously and correctly the runtime value is actually a ZodArray already, it's only the type that isn't.

nathankleyn avatar Jun 04 '24 08:06 nathankleyn

Running into this too -- having drizzle-zod work on arrays is crucial functionality! Hoping this gets fixed soon :)

slimshreydy avatar Sep 22 '24 07:09 slimshreydy

The thing I just noticed is that it works properly but the types are off. Took we a while of breaking the schema to realise that

tobychidi avatar Oct 04 '24 05:10 tobychidi

Also running into this issue.

ngajhede avatar Oct 15 '24 15:10 ngajhede

Possibly duplicated by https://github.com/drizzle-team/drizzle-orm/issues/1609.

Heads-up drizzle team, there may be a workaround for this, but it's an issue with one of the fundamental value propositions of the library.

Seems like there's a PR for this that hasn't been reviewed yet.

extradosages avatar Nov 06 '24 18:11 extradosages

This one should be fixed in the latest [email protected]

AndriiSherman avatar Dec 09 '24 15:12 AndriiSherman