Fix recursive object initialization errors with check() and other methods #5018
+69
−3
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.
Summary
This PR fixes
ReferenceError: Cannot access before initialization
errors that occur when using methods like.check()
,.catchall()
,.passthrough()
, etc. on recursive object schemas.Problem
When defining recursive objects and using certain methods, the object spread syntax (
{...shape}
) causes initialization errors:The issue occurs because object spread syntax immediately evaluates all properties, including getters. When the getter references a recursive type that's still being initialized, it triggers a ReferenceError.
Solution
Replace object spread with a new
util.objectClone()
function that usesObject.create()
withObject.getOwnPropertyDescriptors()
to properly clone objects while preserving property descriptors (including getters) without evaluating them immediately.This approach:
Changes
util.objectClone()
function that properly clones objects with descriptorsobject()
,strictObject()
, andlooseObject()
to useobjectClone()
instead of object spread.check()
methodTesting
All existing tests pass, plus new test case for recursive objects with
.check()
method.🤖 Generated with Claude Code