Vithrax
Vithrax
Explore posts from servers
DTDrizzle Team
Created by Vithrax on 2/11/2024 in #help
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.
3 replies
TTCTheo's Typesafe Cult
Created by Vithrax on 9/5/2023 in #questions
tailwind error?
Hey I have a weird... Bug? i think. It looks like hidden class has presedence over the md:flex? https://gyazo.com/715ab2f6df0172b4c3bbf730e83e8aa6 When i manually turn off hidden then it looks ok Any suggestion guys?
3 replies
TTCTheo's Typesafe Cult
Created by Vithrax on 8/25/2023 in #questions
Zod object validator using Prisma (MySQL) enum
Hey, could anyone hint how to create a zod validator object, which one of the keys should be an enum from prisma (mysql)
model ProposalOption {
id Int @id @default(autoincrement())
description String @db.TinyText
category OptionCategory
active Boolean
}

enum OptionCategory {
reason
size
feature
additional
}
model ProposalOption {
id Int @id @default(autoincrement())
description String @db.TinyText
category OptionCategory
active Boolean
}

enum OptionCategory {
reason
size
feature
additional
}
zod validator (here is the issue, please help)
import z from "zod";
import type { OptionCategory } from "@prisma/client";

const options: OptionCategory[] = ["additional", "feature", "reason", "size"];

export const CreateProposalOptionValidator = z.object({
description: z
.string()
.min(5, { message: "Description must have at least 5 characters" })
.max(100, { message: "Description must have less than 100 characters" }),
// @ts-expect-error TODO: fix this, just a string atm
category: z.enum(options),
});

export type ProposalOptionCreateRequest = z.infer<
typeof CreateProposalOptionValidator
>;
import z from "zod";
import type { OptionCategory } from "@prisma/client";

const options: OptionCategory[] = ["additional", "feature", "reason", "size"];

export const CreateProposalOptionValidator = z.object({
description: z
.string()
.min(5, { message: "Description must have at least 5 characters" })
.max(100, { message: "Description must have less than 100 characters" }),
// @ts-expect-error TODO: fix this, just a string atm
category: z.enum(options),
});

export type ProposalOptionCreateRequest = z.infer<
typeof CreateProposalOptionValidator
>;
when parsing body on backend using this validator, category is just a string, which causes another type error on the backend when creating a new record pls help!
2 replies