DT
Drizzle Team•3mo ago
jorge

drizzle-zod createInsertSchema gives optional types

I have the following table:
export const Recipe = pgTable("recipe", {
id: uuid("id").notNull().primaryKey().defaultRandom(),
title: varchar("title", { length: 256 }).notNull(),
description: text("description").notNull(),
mainImage: varchar("main_image", { length: 512 }),
ingredientNames: varchar("ingredient_names", { length: 255 }).array().notNull(), // Array of ingredient names
ingredientQuantities: real("ingredient_quantities").array().notNull(), // Array of ingredient quantities
ingredientUnits: varchar("ingredient_units", { length: 20 }).array().notNull(), // Array of ingredient units
stepDescriptions: text("step_descriptions").array().notNull(), // Array of step descriptions
stepImages: varchar("step_images", { length: 512 }).array().notNull(), // Array of step images
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at", {
mode: "date",
withTimezone: true,
}).$onUpdateFn(() => sql`now()`),
postedBy: uuid("posted_by").references(() => User.id, { onDelete: "cascade" }),
});
export const Recipe = pgTable("recipe", {
id: uuid("id").notNull().primaryKey().defaultRandom(),
title: varchar("title", { length: 256 }).notNull(),
description: text("description").notNull(),
mainImage: varchar("main_image", { length: 512 }),
ingredientNames: varchar("ingredient_names", { length: 255 }).array().notNull(), // Array of ingredient names
ingredientQuantities: real("ingredient_quantities").array().notNull(), // Array of ingredient quantities
ingredientUnits: varchar("ingredient_units", { length: 20 }).array().notNull(), // Array of ingredient units
stepDescriptions: text("step_descriptions").array().notNull(), // Array of step descriptions
stepImages: varchar("step_images", { length: 512 }).array().notNull(), // Array of step images
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at", {
mode: "date",
withTimezone: true,
}).$onUpdateFn(() => sql`now()`),
postedBy: uuid("posted_by").references(() => User.id, { onDelete: "cascade" }),
});
And I am using drizzle-zod to generate a Zod type from my schema:
export const CreateRecipeSchema = createInsertSchema(Recipe).omit({
id: true,
createdAt: true,
updatedAt: true,
});
export const CreateRecipeSchema = createInsertSchema(Recipe).omit({
id: true,
createdAt: true,
updatedAt: true,
});
However when I try to use the type within my tRPC procedure as such, I get a typescript error, which seems to be complaining that the properties within CreateRecipeSchema are optional:
create: publicProcedure
.input(CreateRecipeSchema)
.mutation(async ({ input, ctx }) => {
const newRecipe = await ctx.db.insert(Recipe).values(input);

return newRecipe;
}),
create: publicProcedure
.input(CreateRecipeSchema)
.mutation(async ({ input, ctx }) => {
const newRecipe = await ctx.db.insert(Recipe).values(input);

return newRecipe;
}),
I'm quite new to drizzle and zod, and from what I gather from documentation, I am using zod correctly, but I am not sure.
6 Replies
jorge
jorge•3mo ago
Here is the typescript error
rphlmr âš¡
rphlmr ⚡•3mo ago
👋 I have the correct ouput using your table + zod schema. Can you share your tsconfig or check you have "strict": true in your tsconfig "compilerOptions"?
{
"compilerOptions": {
"strict": true, // this
}
}
{
"compilerOptions": {
"strict": true, // this
}
}
No description
Musa
Musa•3d ago
I have a similar issue, I changed my tsconfig and still running in to a similar problem
rphlmr âš¡
rphlmr ⚡•3d ago
@Musa There is also strictNullChecks to set to true We are tracking every GitHub report to update the documentation. Depending on which template or framework you use, there are many different configurations.
Musa
Musa•3d ago
This worked. Thanks alot. My bad i asked this question earlier without checking to see if other people had the same issue.
rphlmr âš¡
rphlmr ⚡•3d ago
No prob 🫡 and welcome!
Want results from more Discord servers?
Add your server