[BUG]: drizzle-zod 0.8.1 createInsertSchema / zod.infer not returning correct type
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
Fix Type Inference Issue in drizzle-zod 0.8.1 with createInsertSchema
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
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.
bashnpm install drizzle-zod@latest - 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
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.
typescriptconst schema = createInsertSchema({ name: z.string().refine(val => val.length > 0, { message: 'Name cannot be empty' }) }); - 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.
typescripttype InsertType = z.infer<typeof schema>; - 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
Alex Chen
2450 rep