-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(record): handle non-function constructor field in isPlainObject #5098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jiji-hoon96
wants to merge
2
commits into
colinhacks:main
Choose a base branch
from
jiji-hoon96:fix/constructor-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { test } from "vitest"; | ||
| import { expect, test } from "vitest"; | ||
| import * as z from "zod/v4"; | ||
|
|
||
| test("extend chaining preserves and overrides properties", () => { | ||
|
|
@@ -16,3 +16,44 @@ test("extend chaining preserves and overrides properties", () => { | |
|
|
||
| schema3.parse({ email: "[email protected]" }); | ||
| }); | ||
|
|
||
| test("extend with constructor field in shape", () => { | ||
| const baseSchema = z.object({ | ||
| name: z.string(), | ||
| }); | ||
|
|
||
| const extendedSchema = baseSchema.extend({ | ||
| constructor: z.string(), | ||
| age: z.number(), | ||
| }); | ||
|
|
||
| const result = extendedSchema.parse({ | ||
| name: "John", | ||
| constructor: "Person", | ||
| age: 30, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ | ||
| name: "John", | ||
| constructor: "Person", | ||
| age: 30, | ||
| }); | ||
|
|
||
| const testCases = [ | ||
| { name: "Test", constructor: 123, age: 25 }, | ||
| { name: "Test", constructor: null, age: 25 }, | ||
| { name: "Test", constructor: true, age: 25 }, | ||
| { name: "Test", constructor: {}, age: 25 }, | ||
| ]; | ||
|
|
||
| for (const testCase of testCases) { | ||
| const anyConstructorSchema = baseSchema.extend({ | ||
| constructor: z.any(), | ||
| age: z.number(), | ||
| }); | ||
|
|
||
| expect(() => anyConstructorSchema.parse(testCase)).not.toThrow(); | ||
| const parsed = anyConstructorSchema.parse(testCase); | ||
| expect(parsed).toEqual(testCase); | ||
| } | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { expect, test } from "vitest"; | ||
| import * as z from "zod/v4"; | ||
|
|
||
| test("record should parse objects with non-function constructor field", () => { | ||
| const schema = z.record(z.string(), z.any()); | ||
|
|
||
| expect(() => schema.parse({ constructor: "string", key: "value" })).not.toThrow(); | ||
|
|
||
| const result1 = schema.parse({ constructor: "string", key: "value" }); | ||
| expect(result1).toEqual({ constructor: "string", key: "value" }); | ||
|
|
||
| expect(() => schema.parse({ constructor: 123, key: "value" })).not.toThrow(); | ||
|
|
||
| const result2 = schema.parse({ constructor: 123, key: "value" }); | ||
| expect(result2).toEqual({ constructor: 123, key: "value" }); | ||
|
|
||
| expect(() => schema.parse({ constructor: null, key: "value" })).not.toThrow(); | ||
|
|
||
| const result3 = schema.parse({ constructor: null, key: "value" }); | ||
| expect(result3).toEqual({ constructor: null, key: "value" }); | ||
|
|
||
| expect(() => schema.parse({ constructor: {}, key: "value" })).not.toThrow(); | ||
|
|
||
| const result4 = schema.parse({ constructor: {}, key: "value" }); | ||
| expect(result4).toEqual({ constructor: {}, key: "value" }); | ||
|
|
||
| expect(() => schema.parse({ constructor: [], key: "value" })).not.toThrow(); | ||
|
|
||
| const result5 = schema.parse({ constructor: [], key: "value" }); | ||
| expect(result5).toEqual({ constructor: [], key: "value" }); | ||
|
|
||
| expect(() => schema.parse({ constructor: true, key: "value" })).not.toThrow(); | ||
|
|
||
| const result6 = schema.parse({ constructor: true, key: "value" }); | ||
| expect(result6).toEqual({ constructor: true, key: "value" }); | ||
| }); | ||
|
|
||
| test("record should still work with normal objects", () => { | ||
| const schema = z.record(z.string(), z.string()); | ||
|
|
||
| expect(() => schema.parse({ normalKey: "value" })).not.toThrow(); | ||
|
|
||
| const result1 = schema.parse({ normalKey: "value" }); | ||
| expect(result1).toEqual({ normalKey: "value" }); | ||
|
|
||
| expect(() => schema.parse({ key1: "value1", key2: "value2" })).not.toThrow(); | ||
|
|
||
| const result2 = schema.parse({ key1: "value1", key2: "value2" }); | ||
| expect(result2).toEqual({ key1: "value1", key2: "value2" }); | ||
| }); | ||
|
|
||
| test("record should validate values according to schema even with constructor field", () => { | ||
| const stringSchema = z.record(z.string(), z.string()); | ||
|
|
||
| expect(() => stringSchema.parse({ constructor: "string", key: "value" })).not.toThrow(); | ||
|
|
||
| expect(() => stringSchema.parse({ constructor: 123, key: "value" })).toThrow(); | ||
| }); | ||
|
|
||
| test("record should work with different key types and constructor field", () => { | ||
| const enumSchema = z.record(z.enum(["constructor", "key"]), z.string()); | ||
|
|
||
| expect(() => enumSchema.parse({ constructor: "value1", key: "value2" })).not.toThrow(); | ||
|
|
||
| const result = enumSchema.parse({ constructor: "value1", key: "value2" }); | ||
| expect(result).toEqual({ constructor: "value1", key: "value2" }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 380 could replace line 378, instead of having both, since this check includes the case where
ctor === undefined(since in that casetypeof ctoris"undefined", which does not equal"function"). But the extra clarity doesn't hurt anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're suggestion is technically correct - line 380 (typeof ctor !== "function") would handle both cases since typeof undefined returns "undefined", not "function".
However, I'd keep both lines for clarity. The explicit undefined check makes the intent more obvious and doesn't hurt performance.
It's a style preference, and the current approach is more readable.