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

[BUG]: drizzle-zod 0.8.1 createInsertSchema / zod.infer not returning correct type

Fresh3 days ago
Mar 14, 20260 views
Confidence Score62%
62%

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.43.1 What version of `drizzle-kit` are you using? 0.31.1 Other packages drizzle-zod@0.8.1 zod@3.25.1 Describe the Bug Drizzle-Zod createInsertSchema() does not create a valid schema. Subsequet `z.infer<typeof Schema> ` throw an error. example [code block] error of first infer usage: [code block] error of second infer usage: [code block] tsconfig [code block] Thanks a lot for any help on this!

Error Output

error of first infer usage:

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 drizzle-zod 0.8.1 with createInsertSchema

Medium Risk

The issue arises from the way the createInsertSchema function generates types that are incompatible with zod's inference mechanism. This can occur due to mismatched types or incorrect schema definitions that do not align with the expected structure, leading to errors during type inference.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update drizzle-zod to latest version

    Ensure you are using the latest version of drizzle-zod, as the issue may have been addressed in a newer release. Check the changelog for any breaking changes or fixes related to createInsertSchema.

    bash
    npm install drizzle-zod@latest
  2. 2

    Review createInsertSchema usage

    Double-check the parameters being passed to createInsertSchema. Ensure that the schema definition aligns with the expected input types. Pay attention to any optional fields or type mismatches that could lead to inference errors.

    typescript
    // Example of correct usage
    const schema = createInsertSchema({ name: z.string(), age: z.number() });
  3. 3

    Use zod's refine method for complex types

    If your schema includes complex types or validations, consider using zod's refine method to ensure that the types are correctly inferred. This can help in cases where the default inference does not capture the intended structure.

    typescript
    const schema = createInsertSchema({ name: z.string().refine(val => val.length > 0, { message: 'Name cannot be empty' }) });
  4. 4

    Test Type Inference

    After making the above changes, test the type inference using z.infer. Ensure that it does not throw any errors and returns the expected type structure.

    typescript
    type InsertType = z.infer<typeof schema>;
  5. 5

    Check TypeScript Configuration

    Ensure that your TypeScript configuration (tsconfig.json) is set up correctly. Pay particular attention to strict mode settings that might affect type inference. Consider enabling 'strictFunctionTypes' and 'noImplicitAny' for better type safety.

    json
    {
      "compilerOptions": {
        "strict": true,
        "strictFunctionTypes": true,
        "noImplicitAny": true
      }
    }

Validation

To confirm the fix worked, run your TypeScript compiler and ensure there are no type errors related to z.infer. Additionally, validate that the generated schema behaves as expected during runtime.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

drizzleormtypescriptbug