schema validation enforcement

const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
The above schema is nice and well - but how can I enforce that email actually only allows to store strings that are emails? So I looked at drizzle-zod but it looks like I would have to define the schema twice?
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
There is no way to attach this to the table schema itself? What about using postgres constraints? https://www.postgresql.org/docs/current/ddl-constraints.html
PostgreSQL Documentation
5.4. Constraints
5.4. Constraints # 5.4.1. Check Constraints 5.4.2. Not-Null Constraints 5.4.3. Unique Constraints 5.4.4. Primary Keys 5.4.5. Foreign Keys 5.4.6. Exclusion Constraints …
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server