fix(drizzle-zod): fix coerce schema input types and inverted integer condition for Zod 4#5675
Open
DORI2001 wants to merge 2 commits intodrizzle-team:mainfrom
Open
fix(drizzle-zod): fix coerce schema input types and inverted integer condition for Zod 4#5675DORI2001 wants to merge 2 commits intodrizzle-team:mainfrom
DORI2001 wants to merge 2 commits intodrizzle-team:mainfrom
Conversation
…condition for Zod 4 In Zod 4, `z.coerce.*()` defaults its generic `T` to `unknown`, causing `createInsertSchema` to infer `unknown` for all coerced fields instead of the expected primitive types (boolean, number, string, bigint, Date). Fix by passing explicit generics: `z.coerce.boolean<boolean>()`, `z.coerce.number<number>()`, `z.coerce.date<Date>()`, `z.coerce.bigint<bigint>()`, `z.coerce.string<string>()`. Also fix an inverted condition in `numberColumnToSchema` where `integer ? z.coerce.number() : z.coerce.number().int()` was applying `.int()` to non-integer columns and omitting it for integer columns. Closes drizzle-team#5659 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
this change is not not enough you will have to change the return types in column.type.ts as well. |
The type-level definitions in column.types.ts also used the bare z.coerce.ZodCoerced* interfaces, which default T to unknown. Add explicit generics to match the runtime fix.
Author
|
Good catch — pushed an update that also adds the explicit generics to the type definitions in |
|
cool. this should work now. hope it merges soon. |
Author
|
@Sukairo-02 — gentle ping on this one! All CI checks are passing and a community member confirmed the fix looks correct. Happy to make any adjustments if needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #5659
When using
createSchemaFactory({ coerce: true })orcreateInsertSchemawith coerce options in Zod 4, the TypeScript input types for coerced fields areunknowninstead of the expected primitive types.In Zod 4,
z.coerce.boolean()and friends default their genericTtounknown, soz.input<typeof insertSchema>returnsunknownfor every coerced field.There's also an inverted condition in
numberColumnToSchema:.int()was being applied to non-integer columns and skipped for integer ones.Fix
Pass explicit generics to all
z.coerce.*()calls so the input type is preserved:z.coerce.boolean<boolean>(),z.coerce.date<Date>(),z.coerce.number<number>(),z.coerce.bigint<bigint>(),z.coerce.string<string>()Fix the inverted integer condition in
numberColumnToSchema:Update tests to assert correct input types on both
createSelectSchemaandcreateInsertSchemawith coerce enabled.