-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix handleColumns
usage of factory.zodInstance
#4865
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
base: main
Are you sure you want to change the base?
Fix handleColumns
usage of factory.zodInstance
#4865
Conversation
756a75e
to
65b104e
Compare
The `handleColumns` used in `drizzle-zod` now uses the `zodInstance` passed in to the `factory` options. The previous implementation caused `createSchemaFactory` to return its methods using `z.object(columnSchemas)` ignoring the custom zod instance passed to the `CreateSchemaFactoryOptions`.
Would there also be a way to allow "refinements" on createSelectSchema, createInsertSchema, createUpdateSchema to be shared? eg we describe a field with zod schema refinements, open-api description, etc, and those are propagated to the corresponding schemas of createSelectSchema, createInsertSchema, createUpdateSchema obviously it should take into account that, eg, fields that have a default value and are not nullable are always set in "selectSchema", but are optional in "createSchema", etc |
I see what you're saying, but refinements should generally work well with the generated schemas as this change doesn't touch on the logic that applies refinements to them, and should only change the reference to the I just saw that |
Move the `zod` instance reference to the top of the function and use it on undefined columns to convert them to `z.ZodAny`
This change only re-implements the schemas for `literalSchema`, `jsonSchema` and `bufferSchema` in `columnToSchema` to use the `factory?.zodInstance` passed in to the `columnToSchema` function
handleColumns
use of factory.zodInstance
handleColumns
usage of factory.zodInstance
I've been testing the changes and they work in the practical sense. The types are wrong though and I can't seem to get autocomplete working from the import { describe, it, expect } from "bun:test";
import { createSchemaFactory } from "drizzle-zod";
import { z } from "@hono/zod-openapi";
import { json, mysqlTable, varchar } from "drizzle-orm/mysql-core";
const { createInsertSchema, createSelectSchema, createUpdateSchema } =
createSchemaFactory({
zodInstance: z,
});
const TestSchema = mysqlTable("test", {
id: varchar({ length: 36 }).notNull(),
json: json().notNull(),
});
const TestInsertSchema = createInsertSchema(TestSchema);
const TestSelectSchema = createSelectSchema(TestSchema);
const TestUpdateSchema = createUpdateSchema(TestSchema);
describe("createInsertSchema usage of the zod instance", () => {
it("should have openapi method on TestInsertSchema", () => {
expect(TestInsertSchema.openapi).toBeDefined();
expect(typeof TestInsertSchema.openapi).toBe("function");
expect(TestInsertSchema.openapi).toBeInstanceOf(Function);
});
});
describe("createSelectSchema usage of the zod instance", () => {
it("should have openapi method on TestSelectSchema", () => {
expect(TestSelectSchema.openapi).toBeDefined();
expect(typeof TestSelectSchema.openapi).toBe("function");
expect(TestSelectSchema.openapi).toBeInstanceOf(Function);
expect(TestSelectSchema.shape).toHaveProperty("id");
});
});
describe("createUpdateSchema usage of the zod instance", () => {
it("should have openapi method on TestUpdateSchema", () => {
expect(TestUpdateSchema.openapi).toBeDefined();
expect(typeof TestUpdateSchema.openapi).toBe("function");
expect(TestUpdateSchema.openapi).toBeInstanceOf(Function);
expect(TestUpdateSchema.shape).toHaveProperty("id");
});
});
describe("schemas must be generated correctly", () => {
it("should generate TestInsertSchema with correct properties", () => {
expect(TestInsertSchema.shape).toHaveProperty("id");
expect(TestInsertSchema.shape).toHaveProperty("json");
expect(TestInsertSchema.shape.id).toBeInstanceOf(z.ZodString);
expect(TestInsertSchema.shape.json).toBeInstanceOf(z.ZodType);
});
it("should generate TestSelectSchema with correct properties", () => {
expect(TestSelectSchema.shape).toHaveProperty("id");
expect(TestSelectSchema.shape).toHaveProperty("json");
expect(TestSelectSchema.shape.id).toBeInstanceOf(z.ZodString);
expect(TestSelectSchema.shape.json).toBeInstanceOf(z.ZodType);
});
it("should generate TestUpdateSchema with correct properties", () => {
expect(TestUpdateSchema.shape).toHaveProperty("json");
expect(TestUpdateSchema.shape.json).toBeInstanceOf(z.ZodType);
});
}); Tests pass, as we are using the correct instance, but the typing issue persists, giving us:
|
The
handleColumns
used indrizzle-zod
now uses thezodInstance
passed in to thefactory
options.The previous implementation caused
createSchemaFactory
to return its methods usingz.object(columnSchemas)
ignoring the custom zod instance passed to theCreateSchemaFactoryOptions
.Btw, love the work!