fix(transform): type standalone z.transform() ctx as $RefinementCtx#5766
Open
mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
Open
fix(transform): type standalone z.transform() ctx as $RefinementCtx#5766mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
Conversation
…ementCtx The standalone z.transform() factory function typed the callback's ctx parameter as ParsePayload, which doesn't include addIssue(). At runtime, the classic ZodTransform constructor adds addIssue to the payload before invoking the callback, so addIssue is always available. The chained .transform() method on ZodType already correctly types ctx as $RefinementCtx — this brings the standalone factory in line with it. Closes colinhacks#5678
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.
What does this PR do?
Fixes the type of the
ctxparameter in the standalonez.transform()factory function to includeaddIssue().Root cause
The standalone
z.transform((input, ctx) => ...)factory function typedctxasParsePayload, which only includesvalue,issues, andaborted. However, at runtime, the classicZodTransformconstructor mutates the payload to attachaddIssue()before invoking the callback — soctx.addIssue(...)works at runtime but TypeScript doesn't see it.The chained
.transform()method onZodTypealready correctly typesctxas$RefinementCtx(which extendsParsePayloadwithaddIssue). The standalone factory was just missed.Fix
Changed the
ctxparameter type fromcore.ParsePayloadtocore.$RefinementCtx<I>in the standalonetransform()function signature.Tests
Added two tests:
z.transform()withctx.addIssue()correctly surfaces validation errorsctx.addIssueis typed as a function viaexpectTypeOfRelated issue
Closes #5678