Skip to content

Reorganising plugin files#2877

Merged
RobinTail merged 2 commits intomasterfrom
org-plugin-files
Aug 7, 2025
Merged

Reorganising plugin files#2877
RobinTail merged 2 commits intomasterfrom
org-plugin-files

Conversation

@RobinTail
Copy link
Copy Markdown
Owner

@RobinTail RobinTail commented Aug 7, 2025

Following #2869

Summary by CodeRabbit

  • New Features

    • Enhanced Zod schemas with new utility methods: example, deprecated, and label for improved metadata handling.
    • Introduced a brand property for custom schema branding.
    • Added a remap method to object schemas for flexible key transformation.
  • Refactor

    • Reorganized and separated runtime augmentation logic to improve maintainability and clarity.
  • Tests

    • Updated test descriptions for clarity; test logic remains unchanged.
  • Chores

    • Updated internal setup to use the new runtime augmentation module.

@RobinTail RobinTail added the refactoring The better way to achieve the same result label Aug 7, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 7, 2025

Walkthrough

This update removes all runtime augmentation logic from zod-plugin/augmentation.ts, leaving only type/interface declarations. The runtime extension logic is moved to a new zod-plugin/runtime.ts file, which now handles prototype patching for Zod schemas. A new zod-plugin/brand-check.ts introduces a custom Zod check for branding. Imports and test setup are updated accordingly.

Changes

Cohort / File(s) Change Summary
Remove runtime augmentation from augmentation.ts
zod-plugin/augmentation.ts
All runtime logic and prototype patching removed; only type/interface augmentations remain.
Introduce brand check type
zod-plugin/brand-check.ts
New module defining $EZBrandCheck custom Zod check with brand metadata.
Add runtime plugin for Zod
zod-plugin/runtime.ts
New file implementing runtime prototype extensions for Zod schemas: example, deprecated, brand, label, and remap methods/properties.
Update imports and test setup
zod-plugin/index.ts, zod-plugin/vitest.setup.ts
Imports updated to use new runtime module for side effects and test setup.
Test name update
zod-plugin/index.spec.ts
Test suite name changed from "Extended Zod prototypes" to "Augmentation"; no logic changes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ZodSchema (Prototype)
    participant RuntimePlugin
    participant BrandCheck

    User->>ZodSchema (Prototype): Create schema (e.g., z.string())
    activate ZodSchema (Prototype)
    ZodSchema (Prototype)->>RuntimePlugin: .example(), .deprecated(), .brand, .label(), .remap()
    RuntimePlugin->>ZodSchema (Prototype): Attach metadata/methods
    ZodSchema (Prototype)->>BrandCheck: Apply brand check (if .brand used)
    BrandCheck-->>ZodSchema (Prototype): Store brand in schema internals
    deactivate ZodSchema (Prototype)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

  • Detaching Zod plugin #2869: Separates Zod plugin into a standalone package and moves helper methods, closely related to the modularization and detachment of runtime augmentation logic in this PR.

Suggested labels

enhancement, patched

Poem

In the warren of code, a change takes root,
Augmentations pruned, new plugins to boot!
With brands and examples, Zod hops anew,
Prototype patches—oh, what a view!
The schemas now dance with metadata delight,
As rabbits approve, thumping in the night. 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4128ab3 and c58255c.

📒 Files selected for processing (6)
  • zod-plugin/augmentation.ts (1 hunks)
  • zod-plugin/brand-check.ts (1 hunks)
  • zod-plugin/index.spec.ts (1 hunks)
  • zod-plugin/index.ts (1 hunks)
  • zod-plugin/runtime.ts (1 hunks)
  • zod-plugin/vitest.setup.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (17)
📓 Common learnings
Learnt from: RobinTail
PR: RobinTail/express-zod-api#0
File: :0-0
Timestamp: 2025-08-01T09:48:13.742Z
Learning: In express-zod-api, when migrating from Zod v3 to v4, the correct approach for internal type imports is to change from `import type { $ZodType } from "zod/v4/core"` to `import { z } from "zod"` and then use `z.core.$ZodType`. The zod/v4/core module is reexported as z.core by the main zod package, making this a valid and working approach.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2428
File: express-zod-api/src/index.ts:44-44
Timestamp: 2025-05-28T18:58:10.064Z
Learning: The type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avoid TS2742 errors for exported functions like attachRouting, makeRequestMock, testEndpoint, and testMiddleware that have types depending on types/qs. This import provides the reference TypeScript needs to infer portable type names.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2736
File: express-zod-api/tsup.config.ts:12-26
Timestamp: 2025-06-14T16:42:52.972Z
Learning: In express-zod-api tsup configurations, the direct mutation of `options.supported` in the `esbuildOptions` callback is intentional behavior and should not be flagged as a side effect issue.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: The JSONSchema type is not exported from the main "zod" module and must be imported from "zod/v4/core" when using Zod v4. This is acceptable for type-only imports as they don't create runtime dependencies.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: The JSONSchema type is not exported from the main "zod" module and must be imported from "zod/v4/core" when using Zod v4. This is acceptable for type-only imports as they don't create runtime dependencies.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/tests/form-schema.spec.ts:31-31
Timestamp: 2025-05-27T19:27:13.492Z
Learning: Zod version 3.25.0 and later expose the Zod v4 API through the special import paths "zod/v4" and "zod/v4/core", allowing v4 features like .loose() to be used even when the package.json dependency shows a 3.x version.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/documentation-helpers.ts:508-512
Timestamp: 2025-05-28T07:58:09.853Z
Learning: In express-zod-api, when working with Zod's JSON schema override callbacks, using `delete` to mutate `ctx.jsonSchema` is the recommended approach per Zod's official documentation, even if it triggers performance linting warnings. This is preferable to creating copies with `undefined` values, especially for snapshot testing.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2697
File: CHANGELOG.md:5-5
Timestamp: 2025-06-02T21:08:56.475Z
Learning: The `cjs-test` directory in the express-zod-api repository is a test workspace and should be excluded when checking for main project version consistency with changelog entries.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/metadata.ts:5-13
Timestamp: 2025-05-27T20:08:50.699Z
Learning: The `_zod` property in Zod v4 schemas is officially documented and recommended for library authors to differentiate between Zod 3 and Zod 4 schemas at runtime, despite the underscore prefix. This is explicitly mentioned in Zod's library authors documentation and is not a private internal property.
📚 Learning: 2025-08-01T09:48:13.742Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#0
File: :0-0
Timestamp: 2025-08-01T09:48:13.742Z
Learning: In express-zod-api, when migrating from Zod v3 to v4, the correct approach for internal type imports is to change from `import type { $ZodType } from "zod/v4/core"` to `import { z } from "zod"` and then use `z.core.$ZodType`. The zod/v4/core module is reexported as z.core by the main zod package, making this a valid and working approach.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.ts
  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-28T18:58:10.064Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2428
File: express-zod-api/src/index.ts:44-44
Timestamp: 2025-05-28T18:58:10.064Z
Learning: The type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avoid TS2742 errors for exported functions like attachRouting, makeRequestMock, testEndpoint, and testMiddleware that have types depending on types/qs. This import provides the reference TypeScript needs to infer portable type names.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.ts
  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-06-14T16:42:52.972Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2736
File: express-zod-api/tsup.config.ts:12-26
Timestamp: 2025-06-14T16:42:52.972Z
Learning: In express-zod-api tsup configurations, the direct mutation of `options.supported` in the `esbuildOptions` callback is intentional behavior and should not be flagged as a side effect issue.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.ts
  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-27T20:27:17.015Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: The JSONSchema type is not exported from the main "zod" module and must be imported from "zod/v4/core" when using Zod v4. This is acceptable for type-only imports as they don't create runtime dependencies.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.ts
  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-27T19:27:13.492Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/tests/form-schema.spec.ts:31-31
Timestamp: 2025-05-27T19:27:13.492Z
Learning: Zod version 3.25.0 and later expose the Zod v4 API through the special import paths "zod/v4" and "zod/v4/core", allowing v4 features like .loose() to be used even when the package.json dependency shows a 3.x version.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.ts
  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-27T19:35:57.357Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/tests/buffer-schema.spec.ts:32-37
Timestamp: 2025-05-27T19:35:57.357Z
Learning: In the express-zod-api project, tests are run from the `express-zod-api` workspace directory, and the project uses an ESM-first environment without `__dirname`. Relative paths like `../logo.svg` in test files correctly resolve to the repository root due to this test execution context.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.spec.ts
📚 Learning: 2025-06-02T21:08:56.475Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2697
File: CHANGELOG.md:5-5
Timestamp: 2025-06-02T21:08:56.475Z
Learning: The `cjs-test` directory in the express-zod-api repository is a test workspace and should be excluded when checking for main project version consistency with changelog entries.

Applied to files:

  • zod-plugin/vitest.setup.ts
  • zod-plugin/index.spec.ts
📚 Learning: 2025-05-27T20:27:17.015Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: Ramda is correctly listed as a dependency in express-zod-api/package.json, so imports of ramda utilities are properly supported.

Applied to files:

  • zod-plugin/index.ts
  • zod-plugin/augmentation.ts
📚 Learning: 2025-05-27T20:22:30.428Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/tests/zts.spec.ts:160-162
Timestamp: 2025-05-27T20:22:30.428Z
Learning: In express-zod-api/tests/zts.spec.ts, the `Fruits` enum intentionally contains both string and numeric members (Apple = "apple", Banana = "banana", Cantaloupe = "cantaloupe", A = 5) and is used with `z.enum(Fruits)` to test how the system handles mixed enum types. This is by design for testing purposes.

Applied to files:

  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
📚 Learning: 2025-05-27T20:08:50.699Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/metadata.ts:5-13
Timestamp: 2025-05-27T20:08:50.699Z
Learning: The `_zod` property in Zod v4 schemas is officially documented and recommended for library authors to differentiate between Zod 3 and Zod 4 schemas at runtime, despite the underscore prefix. This is explicitly mentioned in Zod's library authors documentation and is not a private internal property.

Applied to files:

  • zod-plugin/index.spec.ts
  • zod-plugin/brand-check.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-27T19:30:51.885Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: compat-test/sample.ts:1-1
Timestamp: 2025-05-27T19:30:51.885Z
Learning: Files in compat-test/ directories, especially those named sample.ts or similar, are often test fixtures for migration scripts and may intentionally contain deprecated or "incorrect" code that the migration tooling is designed to fix. These should not be flagged as issues.

Applied to files:

  • zod-plugin/index.spec.ts
📚 Learning: 2025-05-28T07:58:09.853Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/documentation-helpers.ts:508-512
Timestamp: 2025-05-28T07:58:09.853Z
Learning: In express-zod-api, when working with Zod's JSON schema override callbacks, using `delete` to mutate `ctx.jsonSchema` is the recommended approach per Zod's official documentation, even if it triggers performance linting warnings. This is preferable to creating copies with `undefined` values, especially for snapshot testing.

Applied to files:

  • zod-plugin/index.spec.ts
  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-27T20:03:34.213Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: example/factories.ts:35-42
Timestamp: 2025-05-27T20:03:34.213Z
Learning: The `./example` directory in the express-zod-api repository contains demonstration code for educational purposes only, not intended for production use. Example code can make simplified assumptions for brevity and clarity, and should not be flagged for missing production-level error handling, security measures, or edge case handling.

Applied to files:

  • zod-plugin/index.spec.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-27T20:40:19.548Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:75-87
Timestamp: 2025-05-27T20:40:19.548Z
Learning: In express-zod-api's `flattenIO` function in json-schema-helpers.ts, the `additionalProperties` field is used as a template to generate property schemas for literal property names extracted from `propertyNames.const` and `propertyNames.enum`. Converting boolean `additionalProperties` values to empty objects `{}` via `Object(entry.additionalProperties)` is intentional behavior, as the function only needs property schema templates, not the boolean semantics of `additionalProperties`.

Applied to files:

  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-08-05T14:43:24.702Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2869
File: zod-plugin/package.json:42-44
Timestamp: 2025-08-05T14:43:24.702Z
Learning: Zod version 4 has been officially released and is stable. As of August 2025, the latest published version is 4.0.14 (published July 30, 2025). Zod v4 is available on npm and can be used in peer dependencies with version constraints like "^4.0.0".

Applied to files:

  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
📚 Learning: 2025-05-28T05:04:40.327Z
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/io-schema.ts:5-8
Timestamp: 2025-05-28T05:04:40.327Z
Learning: The Base type `object & { [Symbol.iterator]?: never }` in express-zod-api correctly excludes arrays and other iterables from IOSchema. Arrays are properly rejected because they have a defined Symbol.iterator method which conflicts with the never constraint, as demonstrated by TypeScript compiler errors when attempting to assign z.array() to IOSchema.

Applied to files:

  • zod-plugin/augmentation.ts
  • zod-plugin/runtime.ts
🔇 Additional comments (14)
zod-plugin/brand-check.ts (3)

1-1: LGTM!

The import follows the correct pattern for accessing Zod's internal types, consistent with the project's approach.


3-14: Well-structured interface definitions.

The interfaces correctly extend Zod's internal types and provide appropriate typing for the brand property.


16-27: Clever implementation of runtime-distinguishable branded types.

The approach of using onattach hooks and storing the brand in bag.brand is well-thought-out, ensuring the brand persists through refinements as intended.

zod-plugin/vitest.setup.ts (1)

1-1: Correct import update.

The import change from "./augmentation" to "./runtime" correctly reflects the reorganization of runtime logic.

zod-plugin/index.ts (1)

1-2: Correct dual import structure.

Both imports are necessary: "./augmentation" for type declarations and "./runtime" for the actual prototype patching. The comment clarifies the side-effect nature.

zod-plugin/index.spec.ts (1)

5-5: Test name updated to match terminology.

The rename from "Extended Zod prototypes" to "Augmentation" aligns with the module naming convention.

zod-plugin/augmentation.ts (2)

1-2: Clean separation of types from runtime.

The conversion to type-only imports correctly reflects that this file now contains only type declarations.


11-55: Well-organized type augmentations.

The module augmentations are properly structured with clear documentation. The separation of type declarations from runtime implementation improves maintainability.

zod-plugin/runtime.ts (6)

6-10: LGTM!

The exampleSetter function correctly handles immutable metadata updates by cloning the existing examples array before modification.


12-14: LGTM!

Simple and correct implementation for marking schemas as deprecated.


16-18: LGTM!

Correctly scoped to ZodDefault schemas and properly sets the default label in metadata.


20-25: LGTM!

The brand setter correctly creates a custom check using the $EZBrandCheck class, allowing flexible brand types.


27-43: Well-implemented object remapping logic.

The function correctly handles both mapping objects and functions, preserves immutability through cloning, and respects passthrough settings. The use of R.invoker(0, "clone") for shape cloning is appropriate for Zod schema objects.


45-82: Robust plugin initialization with proper guards.

The initialization correctly:

  • Prevents multiple initializations using a unique symbol flag
  • Filters Zod classes appropriately
  • Uses proper property descriptors for immutability
  • Cleverly overrides the existing brand method using getter/setter pattern
  • Maintains type safety with satisfies operators
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch org-plugin-files

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@RobinTail RobinTail marked this pull request as ready for review August 7, 2025 20:19
@RobinTail RobinTail merged commit eab3b15 into master Aug 7, 2025
13 checks passed
@RobinTail RobinTail deleted the org-plugin-files branch August 7, 2025 20:57
@coderabbitai coderabbitai Bot mentioned this pull request Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring The better way to achieve the same result

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant