Skip to content

omermecitoglu/drizzle-zod-shared-refinements

Repository files navigation

Drizzle Zod Shared Refinements

npm version npm downloads codecov License: MIT GitHub last commit GitHub issues GitHub stars

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-refinements

Usage

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.

About

Provides a helper function for drizzle-zod to create shared refinements

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Generated from omermecitoglu/node-module