From 6f8a6ccfbe0af44a3a8b50717016be873de95ce1 Mon Sep 17 00:00:00 2001 From: Mario564 Date: Tue, 17 Dec 2024 12:45:58 -0800 Subject: [PATCH] Fix infinitely recursive type in drizzle-valibot --- drizzle-valibot/src/column.types.ts | 98 +++++++------------- drizzle-valibot/src/schema.types.internal.ts | 3 +- 2 files changed, 32 insertions(+), 69 deletions(-) diff --git a/drizzle-valibot/src/column.types.ts b/drizzle-valibot/src/column.types.ts index e6cd797ed..b9567b12d 100644 --- a/drizzle-valibot/src/column.types.ts +++ b/drizzle-valibot/src/column.types.ts @@ -30,21 +30,23 @@ export type ExtractAdditionalProperties = { fixedLength: TColumn['_']['columnType'] extends 'PgChar' | 'MySqlChar' | 'PgHalfVector' | 'PgVector' | 'PgArray' ? true : false; - arrayPipelines: []; }; -type RemovePipeIfNoElements> = T extends - infer TPiped extends { pipe: [any, ...any[]] } ? TPiped['pipe'][1] extends undefined ? T['pipe'][0] : TPiped - : never; +type GetLengthAction, TType extends string | ArrayLike> = + T['fixedLength'] extends true ? v.LengthAction + : v.MaxLengthAction; -type BuildArraySchema< - TWrapped extends v.GenericSchema, - TPipelines extends any[][], -> = TPipelines extends [infer TFirst extends any[], ...infer TRest extends any[][]] - ? BuildArraySchema, ...TFirst]>>, TRest> - : TPipelines extends [infer TFirst extends any[]] - ? BuildArraySchema, ...TFirst]>>, []> - : TWrapped; +type GetArraySchema = v.ArraySchema< + GetValibotType< + T['_']['data'], + T['_']['dataType'], + T['_']['columnType'], + GetEnumValuesFromColumn, + GetBaseColumn, + ExtractAdditionalProperties + >, + undefined +>; export type GetValibotType< TData, @@ -53,51 +55,22 @@ export type GetValibotType< TEnumValues extends [string, ...string[]] | undefined, TBaseColumn extends Column | undefined, TAdditionalProperties extends Record, -> = TColumnType extends 'PgHalfVector' | 'PgVector' ? RemovePipeIfNoElements< - v.SchemaWithPipe< - RemoveNeverElements<[ - v.ArraySchema, undefined>, - TAdditionalProperties['max'] extends number - ? TAdditionalProperties['fixedLength'] extends true ? v.LengthAction - : v.MaxLengthAction - : never, - ]> +> = TColumnType extends 'PgHalfVector' | 'PgVector' ? TAdditionalProperties['max'] extends number ? v.SchemaWithPipe< + [v.ArraySchema, undefined>, GetLengthAction] > - > + : v.ArraySchema, undefined> : TColumnType extends 'PgUUID' ? v.SchemaWithPipe<[v.StringSchema, v.UuidAction]> - // PG array handling start - // Nesting `GetValibotType` within `v.ArraySchema` will cause infinite recursion - // The workaround is to accumulate all the array validations (done via `arrayPipelines` in `TAdditionalProperties`) and then build the schema afterwards - : TAdditionalProperties['arrayFinished'] extends true ? GetValibotType< - TData, - TDataType, - TColumnType, - TEnumValues, - TBaseColumn, - Omit - > extends infer TSchema extends v.GenericSchema ? BuildArraySchema - : never - : TBaseColumn extends Column ? GetValibotType< - TBaseColumn['_']['data'], - TBaseColumn['_']['dataType'], - TBaseColumn['_']['columnType'], - GetEnumValuesFromColumn, - GetBaseColumn, - Omit, 'arrayPipelines'> & { - arrayPipelines: [ - RemoveNeverElements<[ - TAdditionalProperties['max'] extends number - ? TAdditionalProperties['fixedLength'] extends true - ? v.LengthAction[], number, undefined> - : v.MaxLengthAction[], number, undefined> - : never, - ]>, - ...TAdditionalProperties['arrayPipelines'], - ]; - arrayFinished: GetBaseColumn extends undefined ? true : false; - } + : TColumnType extends 'PgBinaryVector' ? v.SchemaWithPipe< + RemoveNeverElements<[ + v.StringSchema, + v.RegexAction, + TAdditionalProperties['max'] extends number ? GetLengthAction : never, + ]> > - // PG array handling end + : TBaseColumn extends Column ? TAdditionalProperties['max'] extends number ? v.SchemaWithPipe< + [GetArraySchema, GetLengthAction] + > + : GetArraySchema : ArrayHasAtLeastOneValue extends true ? v.EnumSchema>, undefined> : TData extends infer TTuple extends [any, ...any[]] ? v.TupleSchema< @@ -147,19 +120,10 @@ export type GetValibotType< v.MaxValueAction, ]> : TData extends boolean ? v.BooleanSchema - : TData extends string ? RemovePipeIfNoElements< - v.SchemaWithPipe< - RemoveNeverElements<[ - v.StringSchema, - TColumnType extends 'PgBinaryVector' ? v.RegexAction - : never, - TAdditionalProperties['max'] extends number - ? TAdditionalProperties['fixedLength'] extends true ? v.LengthAction - : v.MaxLengthAction - : never, - ]> - > - > + : TData extends string + ? TAdditionalProperties['max'] extends number + ? v.SchemaWithPipe<[v.StringSchema, GetLengthAction]> + : v.StringSchema : v.AnySchema; type HandleSelectColumn< diff --git a/drizzle-valibot/src/schema.types.internal.ts b/drizzle-valibot/src/schema.types.internal.ts index 57dcedc7c..e39c34d8a 100644 --- a/drizzle-valibot/src/schema.types.internal.ts +++ b/drizzle-valibot/src/schema.types.internal.ts @@ -67,8 +67,7 @@ export type BuildSchema< TType extends 'select' | 'insert' | 'update', TColumns extends Record, TRefinements extends Record | undefined, -> // @ts-ignore false-positive - = v.ObjectSchema< +> = v.ObjectSchema< Simplify< RemoveNever< {