feat: add z.ext namespace for safe plugin extension#5567
Open
Ayush2k02 wants to merge 3 commits intocolinhacks:mainfrom
Open
feat: add z.ext namespace for safe plugin extension#5567Ayush2k02 wants to merge 3 commits intocolinhacks:mainfrom
Ayush2k02 wants to merge 3 commits intocolinhacks:mainfrom
Conversation
Fixes colinhacks#5544 Adds a mutable z.ext namespace to allow community plugins to extend Zod without modifying the frozen core z object. This addresses the issue where direct property assignment fails in production builds. Problem: In issue colinhacks#5544, users reported that direct property assignment to the z namespace (e.g., z.iso.timezone = ...) works in development but throws "TypeError: Cannot assign to read only property" in production builds due to object freezing. Solution: Implemented z.ext as a dedicated, mutable namespace specifically for plugins. This approach: - Is bundler-safe and works in all production environments - Provides a standardized format for community plugins - Maintains discoverability through z.ext.* with IDE autocomplete - Avoids any breaking changes to the core z object Implementation: - Created packages/zod/src/v4/classic/plugins.ts with ext namespace - Exported ext from external.ts to make it available on z - Added comprehensive tests in plugins.test.ts demonstrating usage - Included PLUGIN_API.md documentation with examples and best practices The implementation follows the maintainer's suggestion to use a dedicated extension namespace rather than a full plugin registration system, avoiding the issues that have affected libraries like dayjs. Usage example: ```typescript // Plugin author z.ext.cron = () => z.string().refine(...) // Consumer import 'my-zod-cron-plugin' const schema = z.ext.cron() ```
Contributor
Contributor
Author
|
Hey, any updates on this ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #5544
Adds a mutable
z.extnamespace to allow community plugins to extend Zod without modifying the frozen corezobject. This addresses the issue where direct property assignment fails in production builds.Example of failing code:
Solution
Implemented
z.extas a dedicated, mutable namespace specifically for plugins. This approach:z.ext.*with IDE autocompletezobjectUsage Example
Plugin Author:
Consumer:
Test Results
zobject remains unaffected