Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix drizzle-zod and drizzle-typebox mapping refinements incorrectly #3737

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion drizzle-typebox/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function mapEnumValues(values: string[]) {
return Object.fromEntries(values.map((value) => [value, value]));
}

/** @internal */
export function columnToSchema(column: Column, t: typeof typebox): TSchema {
let schema!: TSchema;

Expand Down
18 changes: 12 additions & 6 deletions drizzle-typebox/src/schema.types.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ export type BuildRefine<
: never;

type HandleRefinement<
TType extends 'select' | 'insert' | 'update',
TRefinement extends t.TSchema | ((schema: t.TSchema) => t.TSchema),
TColumn extends Column,
> = TRefinement extends (schema: t.TSchema) => t.TSchema
? TColumn['_']['notNull'] extends true ? ReturnType<TRefinement>
: t.TTuple<[ReturnType<TRefinement>, t.TNull]>
> = TRefinement extends (schema: any) => t.TSchema ? (TColumn['_']['notNull'] extends true ? ReturnType<TRefinement>
: t.TUnion<[ReturnType<TRefinement>, t.TNull]>) extends infer TSchema
? TType extends 'update' ? t.TOptional<Assume<TSchema, t.TSchema>> : TSchema
: t.TSchema
: TRefinement;

type IsRefinementDefined<TRefinements, TKey extends string> = TKey extends keyof TRefinements
? TRefinements[TKey] extends t.TSchema | ((schema: any) => any) ? true
: false
: false;

export type BuildSchema<
TType extends 'select' | 'insert' | 'update',
TColumns extends Record<string, any>,
Expand All @@ -57,9 +64,8 @@ export type BuildSchema<
{
[K in keyof TColumns]: TColumns[K] extends infer TColumn extends Column
? TRefinements extends object
? TRefinements[Assume<K, keyof TRefinements>] extends
infer TRefinement extends t.TSchema | ((schema: t.TSchema) => t.TSchema)
? HandleRefinement<TRefinement, TColumn>
? IsRefinementDefined<TRefinements, Assume<K, string>> extends true
? HandleRefinement<TType, TRefinements[Assume<K, keyof TRefinements>], TColumn>
: HandleColumn<TType, TColumn>
: HandleColumn<TType, TColumn>
: TColumns[K] extends infer TObject extends SelectedFieldsFlat<Column> | Table | View ? BuildSchema<
Expand Down
1 change: 0 additions & 1 deletion drizzle-valibot/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function mapEnumValues(values: string[]) {
return Object.fromEntries(values.map((value) => [value, value]));
}

/** @internal */
export function columnToSchema(column: Column): v.GenericSchema {
let schema!: v.GenericSchema;

Expand Down
1 change: 0 additions & 1 deletion drizzle-zod/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const jsonSchema: z.ZodType<Json> = z.lazy(() =>
);
export const bufferSchema: z.ZodType<Buffer> = z.custom<Buffer>((v) => v instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof

/** @internal */
export function columnToSchema(column: Column, z: typeof zod): z.ZodTypeAny {
let schema!: z.ZodTypeAny;

Expand Down
18 changes: 12 additions & 6 deletions drizzle-zod/src/schema.types.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ export type BuildRefine<
: never;

type HandleRefinement<
TType extends 'select' | 'insert' | 'update',
TRefinement extends z.ZodTypeAny | ((schema: z.ZodTypeAny) => z.ZodTypeAny),
TColumn extends Column,
> = TRefinement extends (schema: z.ZodTypeAny) => z.ZodTypeAny
? TColumn['_']['notNull'] extends true ? ReturnType<TRefinement>
: z.ZodNullable<ReturnType<TRefinement>>
> = TRefinement extends (schema: any) => z.ZodTypeAny ? (TColumn['_']['notNull'] extends true ? ReturnType<TRefinement>
: z.ZodNullable<ReturnType<TRefinement>>) extends infer TSchema
? TType extends 'update' ? z.ZodOptional<Assume<TSchema, z.ZodTypeAny>> : TSchema
: z.ZodTypeAny
: TRefinement;

type IsRefinementDefined<TRefinements, TKey extends string> = TKey extends keyof TRefinements
? TRefinements[TKey] extends z.ZodTypeAny | ((schema: any) => any) ? true
: false
: false;

export type BuildSchema<
TType extends 'select' | 'insert' | 'update',
TColumns extends Record<string, any>,
Expand All @@ -56,9 +63,8 @@ export type BuildSchema<
{
[K in keyof TColumns]: TColumns[K] extends infer TColumn extends Column
? TRefinements extends object
? TRefinements[Assume<K, keyof TRefinements>] extends
infer TRefinement extends z.ZodTypeAny | ((schema: z.ZodTypeAny) => z.ZodTypeAny)
? HandleRefinement<TRefinement, TColumn>
? IsRefinementDefined<TRefinements, Assume<K, string>> extends true
? HandleRefinement<TType, TRefinements[Assume<K, keyof TRefinements>], TColumn>
: HandleColumn<TType, TColumn>
: HandleColumn<TType, TColumn>
: TColumns[K] extends infer TObject extends SelectedFieldsFlat<Column> | Table | View ? BuildSchema<
Expand Down