https://github.com/omermecitoglu/drizzle-zod-shared-refinements
Provides a helper function for drizzle-zod to create shared refinements
https://github.com/omermecitoglu/drizzle-zod-shared-refinements
drizzle drizzle-zod refinements typescript zod
Last synced: 4 months ago
JSON representation
Provides a helper function for drizzle-zod to create shared refinements
- Host: GitHub
- URL: https://github.com/omermecitoglu/drizzle-zod-shared-refinements
- Owner: omermecitoglu
- License: mit
- Created: 2026-01-09T03:12:06.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2026-01-17T02:52:33.000Z (6 months ago)
- Last Synced: 2026-01-22T07:32:43.987Z (6 months ago)
- Topics: drizzle, drizzle-zod, refinements, typescript, zod
- Language: TypeScript
- Homepage: https://orm.drizzle.team/docs/zod#refinements
- Size: 70.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Drizzle Zod Shared Refinements
[](https://www.npmjs.com/package/drizzle-zod-shared-refinements)
[](https://www.npmjs.com/package/drizzle-zod-shared-refinements)
[](https://codecov.io/gh/omermecitoglu/drizzle-zod-shared-refinements)
[](https://opensource.org/licenses/MIT)
[](https://github.com/omermecitoglu/drizzle-zod-shared-refinements/commits/main/)
[](https://github.com/omermecitoglu/drizzle-zod-shared-refinements/issues)
[](https://github.com/omermecitoglu/drizzle-zod-shared-refinements)
## Overview
This tiny module provides a helper function to define the refinements just once and share between `createSelectSchema`, `createInsertSchema` and `createUpdateSchema`
[Read the official drizzle documentation for more information](https://orm.drizzle.team/docs/zod#refinements)
## Installation
To install this package
```sh
npm install drizzle-zod-shared-refinements
```
## Usage
before you needed to write refinements for each schemas
```typescript
import { pgTable, text, integer, json } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema, createUpdateSchema } from 'drizzle-zod';
import { z } from 'zod/v4';
const users = pgTable('users', {
id: integer().primaryKey(),
name: text().notNull(),
bio: text(),
preferences: json()
});
const userSelectSchema = createSelectSchema(users, {
name: (schema) => schema.max(20), // Extends schema
bio: (schema) => schema.max(1000), // Extends schema before becoming nullable/optional
preferences: z.object({ theme: z.string() }) // Overwrites the field, including its nullability
});
const userInsertSchema = createInsertSchema(users, {
name: (schema) => schema.max(20), // Extends schema
bio: (schema) => schema.max(1000), // Extends schema before becoming nullable/optional
preferences: z.object({ theme: z.string() }) // Overwrites the field, including its nullability
});
const userUpdateSchema = createUpdateSchema(users, {
name: (schema) => schema.max(20), // Extends schema
bio: (schema) => schema.max(1000), // Extends schema before becoming nullable/optional
preferences: z.object({ theme: z.string() }) // Overwrites the field, including its nullability
});
```
now you can share the refinements
```typescript
import { pgTable, text, integer, json } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema, createUpdateSchema } from 'drizzle-zod';
import { createSharedRefinements } from "drizzle-zod-shared-refinements";
import { z } from 'zod/v4';
const users = pgTable('users', {
id: integer().primaryKey(),
name: text().notNull(),
bio: text(),
preferences: json()
});
const sharedRefinements = createSharedRefinements(users, {
name: (schema) => schema.max(20), // Extends schema
bio: (schema) => schema.max(1000), // Extends schema before becoming nullable/optional
preferences: z.object({ theme: z.string() }) // Overwrites the field, including its nullability
});
const userSelectSchema = createSelectSchema(users, sharedRefinements);
const userInsertSchema = createInsertSchema(users, sharedRefinements);
const userUpdateSchema = createUpdateSchema(users, sharedRefinements);
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.