jorge
jorge
Explore posts from servers
DTDrizzle Team
Created by jorge on 8/11/2024 in #help
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.
8 replies