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

[BUG]: `createInsertSchema` from `[email protected]` returns `any` for most values

Open abegehr opened this issue 9 months ago • 3 comments

Report hasn't been filed before.

  • [x] I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.40.1

What version of drizzle-kit are you using?

0.30.6

Other packages

[email protected], [email protected]

Describe the Bug

Similar issue: https://github.com/drizzle-team/drizzle-orm/issues/3907

I'm running into this issue on [email protected] with sqlite

import * as t from "drizzle-orm/sqlite-core";
import { sqliteTable as table } from "drizzle-orm/sqlite-core";
import { createSelectSchema } from "drizzle-zod";

export const Table = table("Table", {
  id: t.int().primaryKey(),
  name: t.text().notNull(),
  age: t.int().notNull(),
  ts: t.int({ mode: "timestamp" }).notNull(),
});
const ZTable = createSelectSchema(Table);
const parsed = ZTable.parse("");

On 0.7.1 except for the timestamp, all other fields are inferred as any:

const parsed: {
    ts: Date;
    id?: any;
    name?: any;
    age?: any;
}

On 0.5.1 it works as expected:

const parsed: {
    id: number;
    name: string;
    age: number;
    ts: Date;
}

All my versions are latest:

{
  "dependencies": {
    "drizzle-orm": "^0.40.1",
    "drizzle-zod": "^0.7.1"
  },
  "devDependencies": {
    "drizzle-kit": "^0.30.6"
  },
  "peerDependencies": {
    "typescript": "^5"
  }
}

abegehr avatar Apr 04 '25 13:04 abegehr

could it be related to what I'm seeing at https://github.com/drizzle-team/drizzle-orm/issues/4361

maelp avatar Apr 04 '25 20:04 maelp

Looks similar, I was also getting unknown as zod-inferred field types at some point, I think. Then I tried to break it down to the simplest non-working version and came up with what I shared above. Seems to be a bug in drizzle-zod > 0.5.1, similar to #3907

abegehr avatar Apr 04 '25 23:04 abegehr

same behavior with deno and postgres

    "drizzle-kit": "npm:drizzle-kit@^0.31.0",
    "drizzle-orm": "npm:drizzle-orm@^0.42.0",
    "drizzle-zod": "npm:drizzle-zod@^0.7.1",

but managed to get it working using the sample on the release notes from 0.7.0 using createSchemaFactory

https://github.com/drizzle-team/drizzle-orm/blob/main/changelogs/drizzle-zod/0.7.0.md#added-type-coercion-support

It's alright, albeit more verbose . It isn't clear to me from all available info if the factory is the intended way to do it, or if createInsertSchema is supposed to be enough.

keponk avatar Apr 26 '25 22:04 keponk

@keponk can you give an an example of how you're using it? I've tried using createSchemaFactory on my side but it didn't work... (my use-case is to add zod-openapi annotations to the fields, and to correctly type the field.jsonb<SomeCustomType>() fields so that when serializing/deserializing those fields they are correctly typed, but for now it doesn't seem to work...

maelp avatar Apr 27 '25 09:04 maelp

@maelp I literally used the example on that link. I could add that I'm also using zod, following this youtube tutorial: https://www.youtube.com/watch?v=sNh9PoM9sUE&t=4670s but as mentioned having switched to deno instead of node. I notice some versions of different parts of the stack have been upgraded since then so I couldn't pin point to the root cause.

I first noticed the issue when using .omit() on the insert (like in the tutorial). I think the issue with omit() might still be there but seems that in the latest version, the fields with default now seem to be automatically interpreted as optional thus eliminating the need of using omit. When I removed it I noticed it was the assigning the correct types (no more anys), but there was an issue for the dates which were being rejected based on the Date vs String types. So went back to the docs and release notes and copied the example using the Factory and now it all works both accepting the date as string, and without any in the validation.

import { createSchemaFactory, createSelectSchema } from "drizzle-zod";

[...]

export const selectMyTestSchema = createMyTestSchema(myTestTable);

const { createInsertSchema } = createSchemaFactory({
  coerce: {
    date: true,
  },
});

export const insertMyTestSchema = createInsertSchema(myTestTable);

Issue is that it still feels awkward, I'm not sure if it is a coincidence, if Deno is/was also failing somewhere in between, or if this is working exactly as it is intended.

keponk avatar Apr 29 '25 10:04 keponk

@keponk ah I see! But I'm not so interested in the "coerce" stuff, rather to add some zod-openapi type annotations, but this seems that it results in a similar bug, but I don't think createSchemaFactory would be useful in my case

maelp avatar Apr 29 '25 11:04 maelp

I’m running into the same issue — InsertCardSchema.shape isn’t accessible or typed in any meaningful way unless I downgrade to [email protected].

With the latest 0.8.x versions, even basic intellisense is missing on the InsertCardSchema object.

hamidbjss avatar May 27 '25 16:05 hamidbjss

@hamidbjss drizzle-zod uses zod 4, (zod/v4). Did you try to installing latest version of zod?

valerii15298 avatar May 27 '25 22:05 valerii15298

Yep, already have it installed

"zod": "^3.25.2"

hamidbjss avatar May 28 '25 08:05 hamidbjss

This should be fixed in the latest version of drizzle-zod as I'm unable to reproduce this issue in the latest version. Make sure you have the latest version of Zod installed as well.

If this issue persists, please feel free to reopen this issue and please provide a reproduction example of this bug.

L-Mario564 avatar Jun 17 '25 15:06 L-Mario564