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! π
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
- π Features
- π Table of Contents
- π― Getting Started
- π Usage
- π API Reference
- π§ͺ Testing
- π» TypeScript Support
- π€ Contributing
- π License
To get started with ZodMod, follow these simple steps:
Make sure you have Zod installed in your project:
npm install zod-modor if you prefer yarn:
yarn add zod-modor pnpm:
pnpm add zod-modimport { 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"]
// }
// ]
}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"
}
]);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"
}
]);The main function to modify Zod schemas dynamically.
function modifySchema<T extends z.ZodTypeAny>(
schema: T,
modifications: Modification[]
): TParameters:
schema- The base Zod schema to modifymodifications- 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"
}
]);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'"
}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:coverageZodMod 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"
}
];Contributions are always welcome!
If you have any ideas, suggestions, or bug reports, please feel free to:
- Open an issue
- Submit a pull request
Before contributing, please read our Contributing Guidelines and Code of Conduct.
# 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 buildThis project is licensed under the MIT License - see the LICENSE file for details.
Made with β€οΈ by Jan Szewczyk
If you find this project useful, please consider giving it a β on GitHub!