Skip to content

Conversation

@mdoi2
Copy link
Contributor

@mdoi2 mdoi2 commented Nov 19, 2025

Issues

Closes #5402

Description

I really like Zod's method chaining style because it's refined and beautiful.
However, I find it a bit disappointing that using external functions breaks this chain.

// ex:
const baseSchema = z.object({ a: z.number(), b: z.string() });
const effectedSchema = typesafeOmit(baseSchema, { b: true });
export const schema = effectedSchema.partial();

To solve this issue, I added .chain() to ZodType.
Using this, you can write as follows.

export const schema = z.object({ a: z.number(), b: z.string() })
  .chain(s => typesafeOmit(s, { b: true }))
  .partial();

Use cases

  • Injecting functions externally that Zod cannot officially implement. (ex: typesafeOmit, typesafePick)
  • Apply common processing to a specific schema.
    (ex: Set common default, min, and max values for an already defined ZodNumber schema.)

P.S.

Following the previous pull request(#5410 Thank you for your review.🙏), I am aware this may also be considered a breaking change. However, since I requested this feature myself, I took responsibility and implemented it.

If you feel this feature is unnecessary, please feel free to reject it.

@colinhacks
Copy link
Owner

cc @pullfrog update this PR

  • rename .chain to .apply
  • add support to ZodMiniType

@pullfrog
Copy link
Contributor

pullfrog bot commented Dec 30, 2025

Completed requested changes to PR #5463

Changes made:

  • Renamed .chain() to .apply() in both interface and implementation (packages/zod/src/v4/classic/schemas.ts)
  • Added .apply() to ZodMiniType in both interface and implementation (packages/zod/src/v4/mini/schemas.ts)
  • Renamed test file from chain.test.ts to apply.test.ts and updated all references
  • Added new test file for Zod Mini (packages/zod/src/v4/mini/tests/apply.test.ts)
  • Updated documentation to reflect new method name and added Zod Mini example (packages/docs/content/api.mdx)

Verification:

  • ✅ All tests pass
  • ✅ Build succeeds

View PR ➔

Pullfrog  | Triggered by Pullfrog | Using Claude CodeView workflow runpullfrog.com𝕏

- Rename the method from `chain` to `apply` in ZodType interface and implementation
- Add `apply` method to ZodMiniType interface and implementation
- Rename test file from chain.test.ts to apply.test.ts and update test content
- Add apply.test.ts for Zod Mini
- Update documentation to reflect new method name and include Zod Mini example
@pullfrog
Copy link
Contributor

pullfrog bot commented Dec 30, 2025

Rename completed

Fixed

  • Removed leftover chain.test.ts file that wasn't deleted during the previous rename

Verification

  • ✅ All tests passing (1385 tests across all test suites)
  • ✅ Type checking passed
  • ✅ Changes pushed to PR

The rename from .chain() to .apply() is now complete.

View PR ➔

Pullfrog  | Triggered by Pullfrog | Using Claude CodeView workflow runpullfrog.com𝕏

The chain method was renamed to apply, but the old test file wasn't deleted.
@colinhacks colinhacks merged commit e5d24c3 into colinhacks:main Dec 30, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Prevents breaking Zod's method chain even when using external functions

2 participants