Skip to content

ZodMod is an extension for Zod that allows you to dynamically modify validation schemas. With it, you can easily add extra rules, such as notEqual, to existing schemas without rewriting them manually.

License

Notifications You must be signed in to change notification settings

JanSzewczyk/zod-mod

ZodMod

ZodMod is an extension for Zod that allows you to dynamically modify validation schemas. With it, you can easily add extra rules, such as notEqual, to existing schemas without rewriting them manually.

This library will save you time and effort in managing complex validation logic, so sit back, relax and get ready to build robust applications with ease! πŸš€

NPM Version NPM Downloads NPM License NPM Type Definitions NPM Peer Dependency Zod


πŸ“š Features

This library is packed with features! πŸ“¦

  • πŸ”„ Dynamic Schema Modification – Modify existing Zod schemas without rewriting them
  • πŸš€ Easy Integration – Seamlessly integrates with your existing Zod validation setup
  • ⚑ Simple Syntax – Clean and intuitive API for adding validation rules
  • πŸ’ͺ TypeScript Support – Full type safety and IntelliSense support
  • 🎯 Extensible – Built to support more validation types in the future
  • πŸ“¦ Lightweight – Minimal overhead, only requires Zod as peer dependency
  • πŸ§ͺ Well Tested – Comprehensive test suite ensures reliability
  • πŸ“– Great Documentation – Clear examples and API reference

πŸ“– Table of Contents

🎯 Getting Started

To get started with ZodMod, follow these simple steps:

Prerequisites

Make sure you have Zod installed in your project:

NPM Peer Dependency Zod

Installation

npm install zod-mod

or if you prefer yarn:

yarn add zod-mod

or pnpm:

pnpm add zod-mod

πŸ“ƒ Usage

Basic Example

import { z } from "zod";
import { modifySchema } from "zod-mod";

const baseSchema = z.object({
  username: z.string(),
  age: z.number()
});

const modifiedSchema = modifySchema(baseSchema, [
  {
    type: "NOT_EQUAL",
    path: "age",
    value: 18,
    errorMessage: "Age cannot be 18"
  }
]);

// This will throw a validation error
const result = modifiedSchema.safeParse({ 
  username: "John", 
  age: 18 
});

if (!result.success) {
  console.log(result.error.issues);
  // Output:
  // [
  //   {
  //     "code": "custom",
  //     "message": "Age cannot be 18",
  //     "path": ["age"]
  //   }
  // ]
}

Multiple Modifications

You can apply multiple modifications to the same schema:

const schema = z.object({
  email: z.string().email(),
  status: z.string(),
  count: z.number()
});

const modifiedSchema = modifySchema(schema, [
  {
    type: "NOT_EQUAL",
    path: "status",
    value: "banned",
    errorMessage: "Status cannot be 'banned'"
  },
  {
    type: "NOT_EQUAL",
    path: "count",
    value: 0,
    errorMessage: "Count must be greater than 0"
  }
]);

Nested Objects

ZodMod supports nested object paths using dot notation:

const schema = z.object({
  user: z.object({
    profile: z.object({
      age: z.number()
    })
  })
});

const modifiedSchema = modifySchema(schema, [
  {
    type: "NOT_EQUAL",
    path: "user.profile.age",
    value: 0,
    errorMessage: "Age must be specified"
  }
]);

πŸ“˜ API Reference

modifySchema

The main function to modify Zod schemas dynamically.

function modifySchema<T extends z.ZodTypeAny>(
  schema: T,
  modifications: Modification[]
): T

Parameters:

  • schema - The base Zod schema to modify
  • modifications - Array of modification objects

Returns:

Modified Zod schema with additional validation rules applied

Example:

const modifiedSchema = modifySchema(baseSchema, [
  {
    type: "NOT_EQUAL",
    path: "fieldName",
    value: "forbiddenValue",
    errorMessage: "Custom error message"
  }
]);

Supported Validations

NOT_EQUAL

Ensures that a field's value is not equal to a specified value.

interface NotEqualModification {
  type: "NOT_EQUAL";
  path: string;           // Dot notation path to the field
  value: any;             // Value that should not be equal
  errorMessage: string;   // Error message if validation fails
}

Example:

{
  type: "NOT_EQUAL",
  path: "username",
  value: "admin",
  errorMessage: "Username cannot be 'admin'"
}

πŸ§ͺ Testing

ZodMod comes with comprehensive test coverage. To run tests:

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Generate coverage report
npm run test:coverage

πŸ’» TypeScript Support

ZodMod is written in TypeScript and provides full type safety:

import { z } from "zod";
import { modifySchema, Modification } from "zod-mod";

// Types are automatically inferred
const schema = z.object({
  name: z.string(),
  age: z.number()
});

type User = z.infer<typeof schema>;
// { name: string; age: number }

// Modification types are checked at compile time
const modifications: Modification[] = [
  {
    type: "NOT_EQUAL",
    path: "age",
    value: 0,
    errorMessage: "Age must be positive"
  }
];

🀝 Contributing

Contributions are always welcome!

If you have any ideas, suggestions, or bug reports, please feel free to:

Before contributing, please read our Contributing Guidelines and Code of Conduct.

Development Setup

# Clone the repository
git clone https://github.com/JanSzewczyk/zod-mod.git

# Install dependencies
npm install

# Run tests
npm test

# Build the project
npm run build

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


⬆ back to top

Made with ❀️ by Jan Szewczyk

If you find this project useful, please consider giving it a ⭐ on GitHub!

About

ZodMod is an extension for Zod that allows you to dynamically modify validation schemas. With it, you can easily add extra rules, such as notEqual, to existing schemas without rewriting them manually.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •