adxvcasas
adxvcasas
PPrisma
Created by adxvcasas on 5/3/2024 in #help-and-questions
Type 'string[]' is not assignable to type 'string'.ts(2322) when trying to create a form using Prism
so I'm trying to create a submitForm using prisma, react-hook-form and zod and mysql as my database. But the problem I'm encountering right now is I get this error on form-submit.ts Type 'string[]' is not assignable to type 'string'.ts(2322) I know this comes from my schema.prisma, because upon reading the docs, on Scalar https://www.prisma.io/docs/orm/reference/prisma-schema-reference#-modifier
model AppoinmentSchedule {
id String @id @default(cuid())
doesHaveTCETAssitance String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model AppoinmentSchedule {
id String @id @default(cuid())
doesHaveTCETAssitance String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Since my zod accepts array.
export const formSchemaData = z.object({
doesHaveTCETAssitance: z.array(z.string()).refine((value) => value.some((item) => item), {
message: 'Please select at lease on the option'
}),

})
export const formSchemaData = z.object({
doesHaveTCETAssitance: z.array(z.string()).refine((value) => value.some((item) => item), {
message: 'Please select at lease on the option'
}),

})
form-submit.ts
export const submitForm = async (
values: z.infer<typeof formSchemaData>
) => {
const validatedFields = formSchemaData.safeParse(values)

if(!validatedFields.success){
return {error: "Form not submitted successfully!"}
}

const {
doesHaveTCETAssitance,
} = validatedFields.data

try {
await db.appoinmentSchedule.create({
data: {
doesHaveTCETAssitance,
}
})
} catch (error) {
return {error: "Something went wrong"}
}
}
export const submitForm = async (
values: z.infer<typeof formSchemaData>
) => {
const validatedFields = formSchemaData.safeParse(values)

if(!validatedFields.success){
return {error: "Form not submitted successfully!"}
}

const {
doesHaveTCETAssitance,
} = validatedFields.data

try {
await db.appoinmentSchedule.create({
data: {
doesHaveTCETAssitance,
}
})
} catch (error) {
return {error: "Something went wrong"}
}
}
3 replies