dannyelo
dannyelo5mo ago

Zod and Convex type error

Hello, I'm using Zod to validate my forms. When I pass the form data to a mutation function, I got a type error. The function expect some convex ids and the zod schema is a simple string. What is the fix here? Zod Schema
const OrderFormSchema = z.object({
salesChannelId: z.string(),
customerId: z.string(),
status: z.string(),
...
})
const OrderFormSchema = z.object({
salesChannelId: z.string(),
customerId: z.string(),
status: z.string(),
...
})
Expecting
args: {
salesChannelId: v.id('sales_channels'),
customerId: v.id('customers'),
status: v.string(),
...
},
args: {
salesChannelId: v.id('sales_channels'),
customerId: v.id('customers'),
status: v.string(),
...
},
Error
Types of property 'customerId' are incompatible.
Type 'string' is not assignable to type 'Id<"customers">'.
Types of property 'customerId' are incompatible.
Type 'string' is not assignable to type 'Id<"customers">'.
3 Replies
dannyelo
dannyeloOP5mo ago
I think I found the solution
createOrder({
...data,
customerId: data.customerId as Id<'customers'>,
salesChannelId: data.salesChannelId as Id<'sales_channels'>,
orderLines: data.orderLines.map((orderLine) => ({
...orderLine,
productId: orderLine.productId as Id<'products'>,
})),
})
createOrder({
...data,
customerId: data.customerId as Id<'customers'>,
salesChannelId: data.salesChannelId as Id<'sales_channels'>,
orderLines: data.orderLines.map((orderLine) => ({
...orderLine,
productId: orderLine.productId as Id<'products'>,
})),
})
ian
ian5mo ago
Zod with TypeScript for Server-side Validation and End-to-End Types
Use Zod with TypeScript for argument validation on your server functions allows you to both protect against invalid data, and define TypeScript types ...
dannyelo
dannyeloOP5mo ago
Thank you @ian!

Did you find this page helpful?