FG
๐Ÿ’ป Software๐Ÿ—„๏ธ Databases

[BUG]: `createInsertSchema` from `drizzle-zod@0.6.1` does not infer types correctly but returns `unknown` for every value

Fresh3 days ago
Mar 14, 20260 views
Confidence Score70%
70%

Problem

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.38.3 What version of `drizzle-kit` are you using? 0.30.1 Other packages drizzle-zod@0.6.1,zod@3.24.1,typescript@5.7.2 Describe the Bug What is the undesired behavior? When using `createInsertSchema` provided by `drizzle-zod` to build a type-safe schema based on a table definition, it seems that the types are not properly inferred when the resulting schema is used to parse data. The keys in the resulting schema overlap with the table but all values are `unknown` (and optional). _Screenshot_ <img width="791" alt="example" src="https://github.com/user-attachments/assets/80a4a8de-5098-44a7-85a4-624bd95a099a" /> What are the steps to reproduce it? 1. Install packages with the following `package.json`: [code block] 2. Copy the example code from the docs. Slightly edited version below: [code block] 3. Launch editor and inspect types ๐Ÿค“ What is the desired result? Ideally I'd like the types to be inferred properly "before" and "after" the use of `createInsertSchema` (and similar helpers) from `drizzle-zod` so that I can use the resulting schema (including specificed overwrites) for type-safe parsing of data.

Error Output

Error: no test specified\" && exit 1"

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Type Inference Issue in createInsertSchema of drizzle-zod

Medium Risk

The issue arises from the way types are inferred in the createInsertSchema function of drizzle-zod. The function does not correctly map the types from the table definition to the resulting schema, leading to all values being inferred as 'unknown'. This is likely due to a mismatch in the expected types and how they are processed within the function.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Type Definitions

    Modify the type definitions in the drizzle-zod package to ensure that the types are correctly inferred from the table schema. This may involve adjusting the generics used in the createInsertSchema function.

    typescript
    type CreateInsertSchema<T> = { [K in keyof T]: T[K] | undefined };
  2. 2

    Add Type Assertions

    In the createInsertSchema function, add type assertions to ensure that the inferred types are correctly interpreted. This can help in resolving the 'unknown' type issue.

    typescript
    const schema = z.object({ ...tableDefinition }) as CreateInsertSchema<typeof tableDefinition>;
  3. 3

    Test with Sample Data

    Create a test case that uses the createInsertSchema function with a sample table definition and validate that the types are inferred correctly. This will help confirm that the changes made are effective.

    typescript
    const schema = createInsertSchema(sampleTableDefinition);
    const result = schema.parse({ column1: 'value', column2: 123 }); // Should not throw an error
  4. 4

    Update Documentation

    Ensure that the documentation for drizzle-zod is updated to reflect the changes made to the type inference process. This will help users understand how to use the createInsertSchema function effectively.

    markdown
    // Update README.md with examples of correct usage
  5. 5

    Publish Updated Package

    Once the changes have been validated, publish the updated drizzle-zod package to npm to ensure that users can access the fix.

    bash
    npm publish

Validation

To confirm the fix worked, run the updated createInsertSchema function with a sample table definition and check that the inferred types are correct. Additionally, ensure that no values are inferred as 'unknown' and that the schema correctly validates the input data.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

drizzleormtypescriptbugdrizzle/zodpriorityhas-pr