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())
})
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()
})
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()
})
3 Replies
Budi
BudiOP2mo ago
And I've found that unless I extend the schema and set many of these properties to be required, the inferred schema considers nullish values acceptable for many of the properties that are set as mandatory on the Postgres schema. Why is this happening? Thank you.
Deps:
"drizzle-kit": "^0.28.1",
"drizzle-orm": "^0.38.4",
"drizzle-zod": "^0.6.1",
Deps:
"drizzle-kit": "^0.28.1",
"drizzle-orm": "^0.38.4",
"drizzle-zod": "^0.6.1",
MarvinKR
MarvinKR4w ago
how do you deal with numeric values? I'm not sure how to extend that so it's respected by the frontend
Budi
BudiOP2w ago
Could you elaborate on your question? I don't get the question unfortunately.

Did you find this page helpful?