From a6f35df02c64883f16d4d676c47ef05ee6e948bd Mon Sep 17 00:00:00 2001 From: swalha1999 Date: Mon, 5 Jan 2026 01:23:39 +0200 Subject: [PATCH] fix(drizzle-zod): apply ZodOptional for insert schema refinements on nullable/default columns When using function refinements in createInsertSchema, the type inference was not correctly applying ZodOptional for: - Nullable columns (should be optional since they can be omitted) - NotNull columns with defaults (should be optional since defaults will be used) This caused a type mismatch where the inferred type was `string | null` but the runtime schema was `string | null | undefined`. The fix aligns HandleRefinement with HandleInsertColumn logic: - For nullable columns: z.ZodOptional> - For notNull with default: z.ZodOptional - For notNull without default: TSchema (required) --- drizzle-zod/src/schema.types.internal.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drizzle-zod/src/schema.types.internal.ts b/drizzle-zod/src/schema.types.internal.ts index 1acee0803c..7f1ae2df38 100644 --- a/drizzle-zod/src/schema.types.internal.ts +++ b/drizzle-zod/src/schema.types.internal.ts @@ -26,7 +26,12 @@ type HandleRefinement< TColumn extends Column, > = TRefinement extends (schema: any) => z.ZodType ? (TColumn['_']['notNull'] extends true ? ReturnType : z.ZodNullable>) extends infer TSchema extends z.ZodType - ? TType extends 'update' ? z.ZodOptional : TSchema + ? TType extends 'update' ? z.ZodOptional + : TType extends 'insert' + ? TColumn['_']['notNull'] extends true + ? TColumn['_']['hasDefault'] extends true ? z.ZodOptional : TSchema + : z.ZodOptional + : TSchema : z.ZodType : TRefinement;