Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]:drizzle-zod doesn't accept custom id value #2957

Closed
patolit opened this issue Sep 12, 2024 · 3 comments
Closed

[BUG]:drizzle-zod doesn't accept custom id value #2957

patolit opened this issue Sep 12, 2024 · 3 comments
Labels
bug Something isn't working drizzle/zod

Comments

@patolit
Copy link

patolit commented Sep 12, 2024

What version of drizzle-orm are you using?

0.33.3

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

I have a table that historically had been defined with custom Id
when we created drizzle we did it with the following value:

export const marketingSmsMessages = mysqlTable( 'marketing_sms_messages', { id: varchar('id', { length: 36 }).notNull(),

When trying to run:
const insertMarketingSmsMessageSchema = createInsertSchema(marketingSmsMessages)

get the following error:
Argument of type 'MySqlTableWithColumns<{ name: "marketing_sms_messages"; schema: undefined; columns: { id: MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages"; dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam: string | number; notNull: true; hasDefault: false; enumValues: [...]; baseColumn: never; ...' is not assignable to parameter of type 'Table<TableConfig<Column<any, object, object>>>'. The types of '_.config.columns' are incompatible between these types. Type '{ id: MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages"; dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam: string | number; notNull: true; hasDefault: false; enumValues: [...]; baseColumn: never; }, object>; ... 16 more ...; updatedAt: MySqlColumn<...>; }' is not assignable to type 'Record<string, Column<any, object, object>>'. Property 'id' is incompatible with index signature. Type 'MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages"; dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam: string | number; notNull: true; hasDefault: false; enumValues: [...]; baseColumn: never; }, object>' is missing the following properties from type 'Column<any, object, object>': generated, generatedIdentity

Expected behavior

I expected the insert schema to just parse the schema in use and give a 1 to 1 zod schema.
as without it I need to manually create zod schema for each table

Environment & setup

No response

@patolit patolit added the bug Something isn't working label Sep 12, 2024
@jadnast
Copy link

jadnast commented Sep 12, 2024

Here you can find more information

It appears that Drizzle ORM's createInsertSchema isn't recognizing the custom id column in your schema automatically. This is likely due to Drizzle ORM's Zod integration not handling non-standard or custom ID fields out of the box.

To resolve this, you can manually specify the schema for the custom id column. This approach ensures the validation for the id field is handled correctly.

Here’s a quick example to illustrate:

import { z } from 'zod';
import { createInsertSchema } from 'drizzle-orm/zod';

const insertMarketingSmsMessageSchema = createInsertSchema(marketingSmsMessages, {
  id: z.string().length(36)  // explicitly defining the custom ID field as a string of length 36
});

By explicitly defining the id field with the correct Zod schema, you're providing the necessary context for validation. In this example, we're defining the id as a string of length 36 (assuming it's a UUID or a similar identifier), which resolves the issue of Drizzle ORM not handling this column automatically.

Let me know if this helps or if you have any further questions!

@patolit
Copy link
Author

patolit commented Sep 15, 2024 via email

@AndriiSherman
Copy link
Member

This one was fixed in the latest drizzle-zod@0.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/zod
Projects
None yet
Development

No branches or pull requests

4 participants