Skip to content

[BUG]: drizzle-zod not parsing arrays correctly #1609

Closed
@jmsims2

Description

What version of drizzle-orm are you using?

0.29.1

What version of drizzle-kit are you using?

0.20.6

Describe the Bug

I have a reproducible example here: https://github.com/jmsims2/drizzle-zod-array-issue

I have a postgres table that has a column defined as text('column').array(). I then create an insert schema for it with createInsertSchema(tableName), however when trying to insert the values into the db, I'm met with a TS error:

No overload matches this call.
  Overload 1 of 2, '(value: { salesChannels?: string[] | SQL<unknown> | Placeholder<string, any> | null | undefined; }): PgInsertBase<PgTableWithColumns<{ name: "test"; schema: undefined; columns: { salesChannels: PgColumn<{ name: "sales_channels"; ... 8 more ...; baseColumn: Column<...>; }, {}, {}>; }; dialect: "pg"; }>, PostgresJsQueryResultHKT, undefined, false, never>', gave the following error.
    Argument of type '{ salesChannels?: string | null | undefined; }' is not assignable to parameter of type '{ salesChannels?: string[] | SQL<unknown> | Placeholder<string, any> | null | undefined; }'.
      Types of property 'salesChannels' are incompatible.
        Type 'string | null | undefined' is not assignable to type 'string[] | SQL<unknown> | Placeholder<string, any> | null | undefined'.

I can get the error to go away by refining the insert schema like this:

export const insertTestSchema2 = createInsertSchema(test, {
  salesChannels: (schema) => schema.salesChannels.array(),
});

However, when using that schema to parse, it fails with this error:

issues: [
    {
      code: 'invalid_type',
      expected: 'array',
      received: 'string',
      path: [Array],
      message: 'Expected array, received string'
    },
    {
      code: 'invalid_type',
      expected: 'array',
      received: 'string',
      path: [Array],
      message: 'Expected array, received string'
    }
  ],

Expected behavior

I'd expect the types to get inferred correctly without having to refine, but when having to refine, I'd expect it to parse correctly.

Environment & setup

https://github.com/jmsims2/drizzle-zod-array-issue

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions