© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•13mo ago•
1 reply
Chev

refine and createInsertSchema

createInsertSchema
createInsertSchema
accepts and option to further refine the schema

I would like to validate fields against each other like ensuring a
completedAt
completedAt
field is date after a
startedAt
startedAt
field.

export const Test = pgTable('test', {
  startedAt: timestamp(timezoneConfig).defaultNow().notNull(),
  completedAt: timestamp(timezoneConfig),
})

    const schema = z
      .object({
        startedAt: z.date().max(new Date()),
        completedAt: z.date().max(new Date()).optional()
      })
      .refine(
        ({ startedAt, completedAt }) => {
          if (completedAt == null) return true

          return completedAt > startedAt
        },
        () => ({
          path: ['startedAt', 'completedAt'],
          message: 'completed date at must be after started date'
        })
      )
export const Test = pgTable('test', {
  startedAt: timestamp(timezoneConfig).defaultNow().notNull(),
  completedAt: timestamp(timezoneConfig),
})

    const schema = z
      .object({
        startedAt: z.date().max(new Date()),
        completedAt: z.date().max(new Date()).optional()
      })
      .refine(
        ({ startedAt, completedAt }) => {
          if (completedAt == null) return true

          return completedAt > startedAt
        },
        () => ({
          path: ['startedAt', 'completedAt'],
          message: 'completed date at must be after started date'
        })
      )
Drizzle ORM - drizzle-zod
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

refine parameters in createInsertSchema not typed properly
Drizzle TeamDTDrizzle Team / help
12mo ago
createInsertSchema
Drizzle TeamDTDrizzle Team / help
2y ago
[postgres][zod] createInsertSchema
Drizzle TeamDTDrizzle Team / help
2y ago
createInsertSchema & createSelectSchema difference
Drizzle TeamDTDrizzle Team / help
3y ago