In my current project, I am utilizing drizzle-orm and zod for validation within a TypeScript environment. The issue I am facing lies in the interaction between these two libraries, specifically when working with the InsertOrderType type provided by drizzle-orm. Here is the relevant code snippet:
// Extracted from drizzle schema
export type InsertOrderType = InferModel<typeof orderSchema, "insert">;
While the InsertOrderType type functions well with drizzle-orm, I require manual calculation of the "amount" field for security reasons and need to exclude it from the zod validation schema. Below is my zod validation schema:
import * as z from 'zod';
const orderSchema = z.object({
// ... other fields ...
// How can I remove "amount" field from this schema?
});
As the "amount" field is not present in the zod validation schema, I am encountering type errors with the InsertOrderType due to the absence of the "amount" field.
I have attempted to mark "amount" as optional in the zod validation schema, but this method does not resolve the issue as it still raises concerns about the missing "amount" in the InsertOrderType.
How should I handle this scenario effectively? Could I be taking the wrong approach with TypeScript types? Is there a way to eliminate the "amount" field from the zod validation schema while maintaining compatibility with drizzle-orm? Any assistance or advice would be highly appreciated.