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!
0 Replies
No replies yetBe the first to reply to this messageJoin