drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Integrate Valibot, Zod and Typebox into custom type definition and JSON column types

Open
#3,565 0 comments 40 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

The validator libraries are easy to integrate with the types that Drizzle has first-party support for.

```ts
varchar({ length: 50 }); /* ---> */ z.string(50);
```

However, a limitation of these libraries is that there's basically no support for custom types and limited support for JSON column types.

```ts
myCustomVarchar({ length: 50 }); /* ---> */ z.any();

json(); /* ---> */ jsonSchema; // Correct, since the JSON column has no shape defined
json().$type<{ prop: string }>(); /* ---> */ jsonSchema; // Incorrect, this schema doesn't respect the shape defined in the `$type` method
```

**The enhancement**

Let developers define the shape of custom types and JSON columns using validators (Zod, Valibot, Typebox).

```ts
const customTextSchema = z.string();
// No need to pass `{ data: string }` since the data type is inferred from the schema passed as the first param
// The schema won't be used at runtime to avoid mixing validator APIs with Drizzle's
const customText = customType.inferTypeFromSchema(customTextSchema, {
dataType() {
return 'text';
},
};

const customJSONSchema = z.object({ prop: z.string() });
// No need to specify `$type` method
const customJSON = json().$inferTypeFromSchema(customJSONSchema);

// The results after passing the to `createSelectSchema` and `createInsertSchema`
customTextColumn; /* ---> */ z.string();
customJSON; /* ---> */ z.object({ prop: z.string() });
```

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.