drizzle-zod-shared-refinements
v2.0.0
Published
Provides a helper function for drizzle-zod to create shared refinements
Downloads
36
Maintainers
Readme
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
Installation
To install this package
npm install drizzle-zod-shared-refinementsUsage
before you needed to write refinements for each schemas
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
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 file for details.
