drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Validation refinements should be united

Open
#3,752 6 comments 3 reactions 0 assignees View on GitHub
drizzle/typebox drizzle/valibot drizzle/zod enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [X] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

Creating refinements for `drizzle-valibot`, `drizzle-zod` or `drizzle-typebox` at the moment requires to produce duplicate code. Example:

```ts
export const products = pgTable('products', {
id: serial().primaryKey(),
name: varchar().notNull(),
slug: varchar().unique().notNull(),
});

export const productInsertSchema = createInsertSchema(products, {
name: (schema) => pipe(schema, minLength(1)),
slug: (schema) =>
pipe(
schema,
minLength(1),
regex(
/^[a-z0-9]+(-[a-z0-9]+)*$/,
'Must be lowercase. Can only contain letters, numbers, and hyphens. Cannot start or end with a hyphen.',
),
),
});

export const productUpdateSchema = createUpdateSchema(products, {
name: (schema) => pipe(schema, minLength(1)),
slug: (schema) =>
pipe(
schema,
minLength(1),
regex(
/^[a-z0-9]+(-[a-z0-9]+)*$/,
'Must be lowercase. Can only contain letters, numbers, and hyphens. Cannot start or end with a hyphen.',
),
),
});
```

A simple solution would be to put the refinements in another const, but then there would be no type-checking and autocomplete for the refinements.

Instead the functions could be unified into one, e.g.:

```ts
export const products = pgTable('products', {
id: serial().primaryKey(),
name: varchar().notNull(),
slug: varchar().unique().notNull(),
});

export const { insertSchema: productsInsertSchema, updateSchema: productsUpdateSchema } = createSchema(products, {
name: (schema) => pipe(schema, minLength(1)),
slug: (schema) =>
pipe(
schema,
minLength(1),
regex(
/^[a-z0-9]+(-[a-z0-9]+)*$/,
'Must be lowercase. Can only contain letters, numbers, and hyphens. Cannot start or end with a hyphen.',
),
),
});
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.