Prisma zod enum validation
I have this prisma schema:
And this zod validation:
enum RealEstateType {
house
apartment
office
garage
land
store
industry
other
}
...
model RealEstate {
id Int @id @default(autoincrement())
status RealEstateStatus
title String
description String
availableFrom DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
user UserData @relation(fields: [userId], references: [userId])
type RealEstateType
contactId String?
contact UserData? @relation(name: "RealEstateContact", fields: [contactId], references: [userId])
favorites Favorite[]
Message Message[]
documents Document[]
energy EnergyEfficiency[]
location RealEstateLocation?
features RealEstateFeatures?
pricing RealEstatePricing?
@@index([userId])
@@index([contactId])
}
enum RealEstateType {
house
apartment
office
garage
land
store
industry
other
}
...
model RealEstate {
id Int @id @default(autoincrement())
status RealEstateStatus
title String
description String
availableFrom DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
user UserData @relation(fields: [userId], references: [userId])
type RealEstateType
contactId String?
contact UserData? @relation(name: "RealEstateContact", fields: [contactId], references: [userId])
favorites Favorite[]
Message Message[]
documents Document[]
energy EnergyEfficiency[]
location RealEstateLocation?
features RealEstateFeatures?
pricing RealEstatePricing?
@@index([userId])
@@index([contactId])
}
const apartmentTypes = [
'house',
'apartment',
'office',
'garage',
'land',
'store',
'industry',
'other'
] as const;
export const createRealEstateSchema = z.object({
type: z.enum(apartmentTypes),
title: z.string().min(3).max(30),
description: z.string().min(3).max(30),
availableFrom: z.date(),
contactId: z.string().optional(),
location: locationSchema.optional(),
features: featuresSchema.optional(),
pricing: pricingSchema.optional(),
documents: z.array(createDocumentSchema),
});
const apartmentTypes = [
'house',
'apartment',
'office',
'garage',
'land',
'store',
'industry',
'other'
] as const;
export const createRealEstateSchema = z.object({
type: z.enum(apartmentTypes),
title: z.string().min(3).max(30),
description: z.string().min(3).max(30),
availableFrom: z.date(),
contactId: z.string().optional(),
location: locationSchema.optional(),
features: featuresSchema.optional(),
pricing: pricingSchema.optional(),
documents: z.array(createDocumentSchema),
});
2 Replies
This is my create route:
But I get this error
How can I properly use the enum type?
export const realEstateRouter = router({
create: protectedProcedure
.input(createRealEstateSchema)
.mutation(async ({ ctx, input }) => {
const userId = ctx.auth.userId;
...
const newRealEstate = await ctx.prisma.realEstate.create({
data: {
...rest,
type: rest.type as unknown as RealEstateType,
status: RealEstateStatus.PENDING,
user: {
connect: {
userId: userId,
}
},
contactId: undefined,
location: {
create: newLocation,
},
features: {
create: newFeatures,
},
pricing: {
create: newPricing,
},
},
});
...
export const realEstateRouter = router({
create: protectedProcedure
.input(createRealEstateSchema)
.mutation(async ({ ctx, input }) => {
const userId = ctx.auth.userId;
...
const newRealEstate = await ctx.prisma.realEstate.create({
data: {
...rest,
type: rest.type as unknown as RealEstateType,
status: RealEstateStatus.PENDING,
user: {
connect: {
userId: userId,
}
},
contactId: undefined,
location: {
create: newLocation,
},
features: {
create: newFeatures,
},
pricing: {
create: newPricing,
},
},
});
...
Argument type: Got invalid value 'apartment' on prisma.createOneRealEstate. Provided String, expected RealEstateTypeCreateNestedOneWithoutRealEstatesInput:
type RealEstateTypeCreateNestedOneWithoutRealEstatesInput {
create?: RealEstateTypeCreateWithoutRealEstatesInput | RealEstateTypeUncheckedCreateWithoutRealEstatesInput
connectOrCreate?: RealEstateTypeCreateOrConnectWithoutRealEstatesInput
connect?: RealEstateTypeWhereUniqueInput
}
Argument type: Got invalid value 'apartment' on prisma.createOneRealEstate. Provided String, expected RealEstateTypeCreateNestedOneWithoutRealEstatesInput:
type RealEstateTypeCreateNestedOneWithoutRealEstatesInput {
create?: RealEstateTypeCreateWithoutRealEstatesInput | RealEstateTypeUncheckedCreateWithoutRealEstatesInput
connectOrCreate?: RealEstateTypeCreateOrConnectWithoutRealEstatesInput
connect?: RealEstateTypeWhereUniqueInput
}
Have you tried z.nativeEnum()?