DT
Drizzle Team•5mo ago
Vithrax

createInsertSchema

Hi, I have small issue with createInsertSchema function. Optional columns can have value or be undefined or null - and null type conflicts with HTMLInputElement attributes. I can't find a way to override the type to be value | undefined. Do you have any suggestions on how to tackle this? Code below
export const recipes = createTable("recipe", {
id: int("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name", { length: 256 }).notNull(),
description: text("description", { length: 256 }),
image: text("image", { length: 256 }),
cookingTime: int("cookingTime", { mode: "number" }),
favorite: int("favorite", { mode: "boolean" }),
createdBy: text("createdBy", { length: 255 })
.notNull()
.references(() => users.id),
createdAt: int("created_at", { mode: "timestamp" })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: int("updatedAt", { mode: "timestamp" }),
});

export const NewRecipeSchema = createInsertSchema(recipes);
export type NewRecipe = z.infer<typeof NewRecipeSchema>;
export const recipes = createTable("recipe", {
id: int("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name", { length: 256 }).notNull(),
description: text("description", { length: 256 }),
image: text("image", { length: 256 }),
cookingTime: int("cookingTime", { mode: "number" }),
favorite: int("favorite", { mode: "boolean" }),
createdBy: text("createdBy", { length: 255 })
.notNull()
.references(() => users.id),
createdAt: int("created_at", { mode: "timestamp" })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: int("updatedAt", { mode: "timestamp" }),
});

export const NewRecipeSchema = createInsertSchema(recipes);
export type NewRecipe = z.infer<typeof NewRecipeSchema>;
NewRecipe type is
type NewRecipe = {
name: string;
createdBy: string;
id?: number | undefined;
description?: string | null | undefined;
image?: string | null | undefined;
cookingTime?: number | null | undefined;
favorite?: boolean | ... 1 more ... | undefined;
createdAt?: Date | undefined;
updatedAt?: Date | ... 1 more ... | undefined;
}
type NewRecipe = {
name: string;
createdBy: string;
id?: number | undefined;
description?: string | null | undefined;
image?: string | null | undefined;
cookingTime?: number | null | undefined;
favorite?: boolean | ... 1 more ... | undefined;
createdAt?: Date | undefined;
updatedAt?: Date | ... 1 more ... | undefined;
}
Just for clarity, i want to change, for example, description to be string | undefined.
2 Replies
Vithrax
Vithrax•5mo ago
to give more context i want to use this schema as validator in react-hook-form (shadcn-ui), would be super convenient to have it auto generated, i expect to modify the table definition quite often maybe i can tackle this differently?
rphlmr âš¡
rphlmr ⚡•5mo ago
👋 You need the type generated by Drizzle and not drizzle-zod:
export type NewRecipe = typeof recipes.$inferInsert;
export type NewRecipeAlt = InferInsertModel<typeof recipes>;
export type NewRecipe = typeof recipes.$inferInsert;
export type NewRecipeAlt = InferInsertModel<typeof recipes>;
https://orm.drizzle.team/docs/goodies#type-api
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.