fix: avoid mutating caller/up-return objects with version key before validation#27
Open
ig-imanish wants to merge 1 commit into
Open
fix: avoid mutating caller/up-return objects with version key before validation#27ig-imanish wants to merge 1 commit into
ig-imanish wants to merge 1 commit into
Conversation
Member
|
Hey @ig-imanish - thanks a lot for opening this PR :) I'm requesting a review again from @mrspence as he is the primary maintainer! |
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.
Description
Problem
The version key (
_versionor custom key) was being assigned directly to the object returned byup()before runningsafeParseon the augmented schema:This caused two issues:
On
ValidationError: the object produced byup()(or the originalstatewhen a mutatingupdidreturn state) would have the version key set even though the migration "failed".On the success path: the exact return value of
up()was mutated as a side-effect (even though the final result came from Zod'sparseResult.data).This affects both
migrate()andmigrateAsync().Solution
Instead of mutating the user's object, we now create a shallow copy only for the validation input:
For plain objects:
{ ...result, [key]: migration.version }For arrays (defensive):
[...result]+ set the property on the copyWe then call
safeParse(toValidate). The successfully migrated state still comes fromparseResult.data(which already contains the version thanks to the.and(z.object({ [key]: literal }))schema).Result:
No side effects on objects returned from
up()No side effects on the caller's original
stateEven when
up()returns the same reference as the previous state, that reference stays clean on errorChanges
src/migrate.ts— core fix in bothmigrate()andmigrateAsync()+ explanatory commentssrc/migrate.spec.ts— 2 new regression testssrc/migrate-async.spec.ts— 2 new regression testsTesting
Reproduced the original bug before the fix (objects were being dirtied)
After the fix: dedicated verification + the 4 new tests pass
Full test run: 44 tests passed
npm run test:types,test:lint,test:format— all cleanCoverage report remains strong
Related
Closes #26
Reviewer note: The shallow copy is minimal and only exists to satisfy the internal version literal requirement of the validation schema. This is the cleanest way to avoid mutation while keeping the existing public API and behavior unchanged for successful migrations.