© 2026 Hedgehog Software, LLC

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

[drizzle-zod] createSchemaFactory unexpected behavior

⚡drizzle-zod
I encounter a weird behavior about drizzle-zod and just putting this here to know if im wrong

import { createInsertSchema, createSchemaFactory } from "drizzle-zod";

export const { createInsertSchema: createInsertSchemaBroken } = createSchemaFactory({
  coerce: true,
});

export const comments = pgTable("comments", {
  postId: integer().notNull(),
  content: text().notNull(),
  userId: text().notNull(),
});

export const commentInsertSchema = createInsertSchema(comments);
export const commentInsertSchemaBroken = createInsertSchemaBroken(comments);


const wrongData = {
  postId: 1,
}

const res1 = commentInsertSchema.safeParse(wrongData);
console.log(res1)
/*
ZodError: [
  {
    "expected": "string",
    "code": "invalid_type",
    "path": [
      "content"
    ],
    "message": "Invalid input: expected string, received undefined"
  },
  {
    "expected": "string",
    "code": "invalid_type",
    "path": [
      "userId"
    ],
    "message": "Invalid input: expected string, received undefined"
  }
]
}
 */


const res2 = commentInsertSchemaBroken.safeParse(wrongData);
console.log(res2)
/*
{
  success: true,
  data: {
    postId: 1,
    content: "undefined",
    userId: "undefined",
  },
}
*/


const trueData = {
  postId: 1,
  content: "This is a comment",
  userId: "user123",
}

const res3 = commentInsertSchema.safeParse(trueData);
console.log(res3)
/*
{
  success: true,
  data: {
    postId: 1,
    content: "This is a comment",
    userId: "user123",
  },
}
*/

const res4 = commentInsertSchemaBroken.safeParse(trueData);
console.log(res4)
/*
{
  success: true,
  data: {
    postId: 1,
    content: "This is a comment",
    userId: "user123",
  },
}
*/
import { createInsertSchema, createSchemaFactory } from "drizzle-zod";

export const { createInsertSchema: createInsertSchemaBroken } = createSchemaFactory({
  coerce: true,
});

export const comments = pgTable("comments", {
  postId: integer().notNull(),
  content: text().notNull(),
  userId: text().notNull(),
});

export const commentInsertSchema = createInsertSchema(comments);
export const commentInsertSchemaBroken = createInsertSchemaBroken(comments);


const wrongData = {
  postId: 1,
}

const res1 = commentInsertSchema.safeParse(wrongData);
console.log(res1)
/*
ZodError: [
  {
    "expected": "string",
    "code": "invalid_type",
    "path": [
      "content"
    ],
    "message": "Invalid input: expected string, received undefined"
  },
  {
    "expected": "string",
    "code": "invalid_type",
    "path": [
      "userId"
    ],
    "message": "Invalid input: expected string, received undefined"
  }
]
}
 */


const res2 = commentInsertSchemaBroken.safeParse(wrongData);
console.log(res2)
/*
{
  success: true,
  data: {
    postId: 1,
    content: "undefined",
    userId: "undefined",
  },
}
*/


const trueData = {
  postId: 1,
  content: "This is a comment",
  userId: "user123",
}

const res3 = commentInsertSchema.safeParse(trueData);
console.log(res3)
/*
{
  success: true,
  data: {
    postId: 1,
    content: "This is a comment",
    userId: "user123",
  },
}
*/

const res4 = commentInsertSchemaBroken.safeParse(trueData);
console.log(res4)
/*
{
  success: true,
  data: {
    postId: 1,
    content: "This is a comment",
    userId: "user123",
  },
}
*/

"@types/pg": "^8.16.0",
"drizzle-kit": "^0.31.8",
"drizzle-orm": "^0.45.1",
"drizzle-zod": "^0.8.3",
"pg": "^8.16.3",
"typescript": "^5.9.3"
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Unexpected behaviour of drizzle-zod createInsertSchema
Drizzle TeamDTDrizzle Team / help
12mo ago
Drizzle-Zod
Drizzle TeamDTDrizzle Team / help
9mo ago
Drizzle-zod errors
Drizzle TeamDTDrizzle Team / help
2y ago
drizzle-zod failing
Drizzle TeamDTDrizzle Team / help
3y ago