Skip to content

Commit

Permalink
Fix schema-to-zod unability to infer schema type (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
romeerez committed Dec 2, 2023
1 parent 2c48976 commit d398140
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-cooks-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'orchid-orm-schema-to-zod': patch
---

Fix schema-to-zod unability to infer schema type
1 change: 1 addition & 0 deletions packages/schema-to-zod/src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('zodSchemaProvider', () => {
columns,
};
}
columns!: typeof columns;
}

const schema = Table.schema();
Expand Down
9 changes: 7 additions & 2 deletions packages/schema-to-zod/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ type MapJsonTuple<T extends unknown[]> = T extends [infer Head, ...infer Tail]

type Columns = { shape: ColumnsShapeBase };
type Table = { columns: ColumnsShapeBase };
type TableClass<T extends Table> = { instance(): T };

type TableClass<T extends Table> = {
new (): T;
// Cannot set T as the `instance()` return type because it cannot be properly inferred
instance(): unknown;
};

export type InstanceToZod<T extends Columns> = z.ZodObject<{
[K in keyof T['shape']]: SchemaToZod<T['shape'][K]>;
Expand All @@ -244,7 +249,7 @@ export const zodSchemaProvider = function <T extends Table>(
this: TableClass<T>,
): InstanceToZod<{ shape: T['columns'] }> {
return instanceToZod({
shape: this.instance().columns,
shape: (this.instance() as T).columns,
}) as unknown as InstanceToZod<{
shape: T['columns'];
}>;
Expand Down

0 comments on commit d398140

Please sign in to comment.