Enforce property requirements on drizzle-zod

export const users = pgTable("user", {
id: text("id").notNull().primaryKey(),
name: text("name"),
email: text("email").notNull(),
emailVerified: timestamp("emailVerified", { mode: "date" }),
image: text("image"),
password: text("password"),
})
export const users = pgTable("user", {
id: text("id").notNull().primaryKey(),
name: text("name"),
email: text("email").notNull(),
emailVerified: timestamp("emailVerified", { mode: "date" }),
image: text("image"),
password: text("password"),
})
password is an optional field,
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import { users } from "@/drizzle/authjs_schema";

// this refers to the entire schema
const insertUserSchema = createInsertSchema(users)


const partial = insertUserSchema.pick({
email: true,
password: true,
}).required()
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import { users } from "@/drizzle/authjs_schema";

// this refers to the entire schema
const insertUserSchema = createInsertSchema(users)


const partial = insertUserSchema.pick({
email: true,
password: true,
}).required()
shouldnt this make email and password required? in the partial schema? I have tried a couple of order different syntaxes with built in methods but nothing seem to work
6 Replies
HotBBQSauce
HotBBQSauceOPā€¢10mo ago
according to docs:
// Overriding the fields
const insertUserSchema = createInsertSchema(users, {
role: z.string(),
});
// Overriding the fields
const insertUserSchema = createInsertSchema(users, {
role: z.string(),
});
does not work!
Mini
Miniā€¢10mo ago
you can overwrite through merge/extend. https://github.com/colinhacks/zod?tab=readme-ov-file#extend
GitHub
GitHub - colinhacks/zod: TypeScript-first schema validation with st...
TypeScript-first schema validation with static type inference - GitHub - colinhacks/zod: TypeScript-first schema validation with static type inference
Mini
Miniā€¢10mo ago
also your methods works on mine
No description
HotBBQSauce
HotBBQSauceOPā€¢10mo ago
can you maybe send a screen of the schema self? i have nullable fields since some columns were not required originally in the schema imma try this out hope this works šŸ™‚
Mini
Miniā€¢10mo ago
Ah required just drop undefined U need to use overwrite with merge
HotBBQSauce
HotBBQSauceOPā€¢10mo ago
Can you give me an example? ā˜ļø
Want results from more Discord servers?
Add your server