11import type { Assume , Column } from 'drizzle-orm' ;
2- import type { z } from 'zod' ;
2+ import type { z } from 'zod/v4 ' ;
33import type { IsEnumDefined , IsNever , Json } from './utils.ts' ;
44
55type HasBaseColumn < TColumn > = TColumn extends { _ : { baseColumn : Column | undefined } }
@@ -9,54 +9,95 @@ type HasBaseColumn<TColumn> = TColumn extends { _: { baseColumn: Column | undefi
99
1010export type GetZodType <
1111 TColumn extends Column ,
12+ TCoerce extends Partial < Record < 'bigint' | 'boolean' | 'date' | 'number' | 'string' , true > > | true | undefined ,
1213> = HasBaseColumn < TColumn > extends true ? z . ZodArray <
13- GetZodType < Assume < TColumn [ '_' ] [ 'baseColumn' ] , Column > >
14+ GetZodType < Assume < TColumn [ '_' ] [ 'baseColumn' ] , Column > , TCoerce >
1415 >
16+ : TColumn [ '_' ] [ 'columnType' ] extends 'PgUUID' ? z . ZodUUID
1517 : IsEnumDefined < TColumn [ '_' ] [ 'enumValues' ] > extends true
16- ? z . ZodEnum < Assume < TColumn [ '_' ] [ 'enumValues' ] , [ string , ...string [ ] ] > >
17- : TColumn [ '_' ] [ 'columnType' ] extends 'PgGeometry' | 'PgPointTuple' ? z . ZodTuple < [ z . ZodNumber , z . ZodNumber ] >
18- : TColumn [ '_' ] [ 'columnType' ] extends 'PgLine' ? z . ZodTuple < [ z . ZodNumber , z . ZodNumber , z . ZodNumber ] >
19- : TColumn [ '_' ] [ 'data' ] extends Date ? z . ZodDate
18+ ? z . ZodEnum < { [ K in Assume < TColumn [ '_' ] [ 'enumValues' ] , [ string , ...string [ ] ] > [ number ] ] : K } >
19+ : TColumn [ '_' ] [ 'columnType' ] extends 'PgGeometry' | 'PgPointTuple' ? z . ZodTuple < [ z . ZodNumber , z . ZodNumber ] , null >
20+ : TColumn [ '_' ] [ 'columnType' ] extends 'PgLine' ? z . ZodTuple < [ z . ZodNumber , z . ZodNumber , z . ZodNumber ] , null >
21+ : TColumn [ '_' ] [ 'data' ] extends Date ? CanCoerce < TCoerce , 'date' > extends true ? z . coerce . ZodCoercedDate : z . ZodDate
2022 : TColumn [ '_' ] [ 'data' ] extends Buffer ? z . ZodType < Buffer >
2123 : TColumn [ '_' ] [ 'dataType' ] extends 'array'
22- ? z . ZodArray < GetZodPrimitiveType < Assume < TColumn [ '_' ] [ 'data' ] , any [ ] > [ number ] > >
24+ ? z . ZodArray < GetZodPrimitiveType < Assume < TColumn [ '_' ] [ 'data' ] , any [ ] > [ number ] , '' , TCoerce > >
2325 : TColumn [ '_' ] [ 'data' ] extends Record < string , any >
2426 ? TColumn [ '_' ] [ 'columnType' ] extends
2527 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson'
26- ? z . ZodType < TColumn [ '_' ] [ 'data' ] , z . ZodTypeDef , TColumn [ '_' ] [ 'data' ] >
27- : z . ZodObject < { [ K in keyof TColumn [ '_' ] [ 'data' ] ] : GetZodPrimitiveType < TColumn [ '_' ] [ 'data' ] [ K ] > } , 'strip' >
28+ ? z . ZodType < TColumn [ '_' ] [ 'data' ] , TColumn [ '_' ] [ 'data' ] >
29+ : z . ZodObject <
30+ { [ K in keyof TColumn [ '_' ] [ 'data' ] ] : GetZodPrimitiveType < TColumn [ '_' ] [ 'data' ] [ K ] , '' , TCoerce > } ,
31+ { } ,
32+ { }
33+ >
2834 : TColumn [ '_' ] [ 'dataType' ] extends 'json' ? z . ZodType < Json >
29- : GetZodPrimitiveType < TColumn [ '_' ] [ 'data' ] > ;
35+ : GetZodPrimitiveType < TColumn [ '_' ] [ 'data' ] , TColumn [ '_' ] [ 'columnType' ] , TCoerce > ;
3036
31- type GetZodPrimitiveType < TData > = TData extends number ? z . ZodNumber
32- : TData extends bigint ? z . ZodBigInt
33- : TData extends boolean ? z . ZodBoolean
34- : TData extends string ? z . ZodString
35- : z . ZodTypeAny ;
37+ type CanCoerce <
38+ TCoerce extends Partial < Record < 'bigint' | 'boolean' | 'date' | 'number' | 'string' , true > > | true | undefined ,
39+ TTo extends 'bigint' | 'boolean' | 'date' | 'number' | 'string' ,
40+ > = TCoerce extends true ? true
41+ : TCoerce extends Record < string , any > ? TCoerce [ TTo ] extends true ? true
42+ : false
43+ : false ;
44+
45+ type GetZodPrimitiveType <
46+ TData ,
47+ TColumnType ,
48+ TCoerce extends Partial < Record < 'bigint' | 'boolean' | 'date' | 'number' | 'string' , true > > | true | undefined ,
49+ > = TColumnType extends
50+ | 'MySqlTinyInt'
51+ | 'SingleStoreTinyInt'
52+ | 'PgSmallInt'
53+ | 'PgSmallSerial'
54+ | 'MySqlSmallInt'
55+ | 'MySqlMediumInt'
56+ | 'SingleStoreSmallInt'
57+ | 'SingleStoreMediumInt'
58+ | 'PgInteger'
59+ | 'PgSerial'
60+ | 'MySqlInt'
61+ | 'SingleStoreInt'
62+ | 'PgBigInt53'
63+ | 'PgBigSerial53'
64+ | 'MySqlBigInt53'
65+ | 'MySqlSerial'
66+ | 'SingleStoreBigInt53'
67+ | 'SingleStoreSerial'
68+ | 'SQLiteInteger'
69+ | 'MySqlYear'
70+ | 'SingleStoreYear' ? CanCoerce < TCoerce , 'number' > extends true ? z . coerce . ZodCoercedNumber : z . ZodInt
71+ : TData extends number ? CanCoerce < TCoerce , 'number' > extends true ? z . coerce . ZodCoercedNumber : z . ZodNumber
72+ : TData extends bigint ? CanCoerce < TCoerce , 'bigint' > extends true ? z . coerce . ZodCoercedBigInt : z . ZodBigInt
73+ : TData extends boolean ? CanCoerce < TCoerce , 'boolean' > extends true ? z . coerce . ZodCoercedBoolean : z . ZodBoolean
74+ : TData extends string ? CanCoerce < TCoerce , 'string' > extends true ? z . coerce . ZodCoercedString : z . ZodString
75+ : z . ZodType ;
3676
3777type HandleSelectColumn <
38- TSchema extends z . ZodTypeAny ,
78+ TSchema extends z . ZodType ,
3979 TColumn extends Column ,
4080> = TColumn [ '_' ] [ 'notNull' ] extends true ? TSchema
4181 : z . ZodNullable < TSchema > ;
4282
4383type HandleInsertColumn <
44- TSchema extends z . ZodTypeAny ,
84+ TSchema extends z . ZodType ,
4585 TColumn extends Column ,
4686> = TColumn [ '_' ] [ 'notNull' ] extends true ? TColumn [ '_' ] [ 'hasDefault' ] extends true ? z . ZodOptional < TSchema >
4787 : TSchema
4888 : z . ZodOptional < z . ZodNullable < TSchema > > ;
4989
5090type HandleUpdateColumn <
51- TSchema extends z . ZodTypeAny ,
91+ TSchema extends z . ZodType ,
5292 TColumn extends Column ,
5393> = TColumn [ '_' ] [ 'notNull' ] extends true ? z . ZodOptional < TSchema >
5494 : z . ZodOptional < z . ZodNullable < TSchema > > ;
5595
5696export type HandleColumn <
5797 TType extends 'select' | 'insert' | 'update' ,
5898 TColumn extends Column ,
59- > = TType extends 'select' ? HandleSelectColumn < GetZodType < TColumn > , TColumn >
60- : TType extends 'insert' ? HandleInsertColumn < GetZodType < TColumn > , TColumn >
61- : TType extends 'update' ? HandleUpdateColumn < GetZodType < TColumn > , TColumn >
62- : GetZodType < TColumn > ;
99+ TCoerce extends Partial < Record < 'bigint' | 'boolean' | 'date' | 'number' | 'string' , true > > | true | undefined ,
100+ > = TType extends 'select' ? HandleSelectColumn < GetZodType < TColumn , TCoerce > , TColumn >
101+ : TType extends 'insert' ? HandleInsertColumn < GetZodType < TColumn , TCoerce > , TColumn >
102+ : TType extends 'update' ? HandleUpdateColumn < GetZodType < TColumn , TCoerce > , TColumn >
103+ : GetZodType < TColumn , TCoerce > ;
0 commit comments