Unexpected behaviour of drizzle-zod createInsertSchema

I have this postgres schema:

export const contractsTable = pgTable('contracts_table', {
    id: integer().primaryKey().generatedByDefaultAsIdentity(),
    title: text().notNull(),
    description: text().notNull(),
    startDate: date().notNull(),
    endDate: date().notNull(),
    noticePeriodUnit: timeIntervals(),
    noticePeriodValue: integer(),
    status: contractStatusEnum().notNull(),
    annualValue: numeric({ precision: 10, scale: 2 }),
    autoRenewing: boolean().notNull().default(true),
    renewalPeriodUnit: timeIntervals(),
    renewalPeriodValue: integer(),
    userId: text()
        .notNull()
        .references(() => usersTable.id, { onDelete: 'cascade' }),
    categoryId: integer().references(() => categoriesTable.id, { onDelete: 'cascade' }),
    notes: text(),
    createdAt: timestamp().notNull().defaultNow(),
    updatedAt: timestamp()
        .notNull()
        .$onUpdate(() => new Date())
})


And this insert schema created with the Drizzle Zod extension:

export const contractFormInsertSchema = createInsertSchema(contractsTable)
    .omit({
        userId: true,
        createdAt: true,
        updatedAt: true
    })
    .extend({
        title: z.string().min(1, 'Title is required'),
        description: z.string().min(1, 'Description is required'),
        startDate: z
            .string({ invalid_type_error: 'Start date is required' })
            .min(1, 'Start date is required'),
        endDate: z
            .string({ invalid_type_error: 'End date is required' })
            .min(1, 'End date is required'),
        noticePeriodUnit: timeIntervalEnumSchema,
        noticePeriodValue: z.number().int().positive(),
        autoRenewing: z.boolean(),
        renewalPeriodUnit: timeIntervalEnumSchema,
        renewalPeriodValue: z.number().int().positive(),
        status: contractStatusEnumSchema.default('active'),
        documents: z.array(fileValidationSchema).optional()
    })
Was this page helpful?