8000 Fix isPlainObject util by antoinechassagne · Pull Request #4574 · colinhacks/zod · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/zod/src/v4/classic/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ test("z.promise", async () => {
test("isPlainObject", () => {
expect(z.core.util.isPlainObject({})).toEqual(true);
expect(z.core.util.isPlainObject(Object.create(null))).toEqual(true);
expect(z.core.util.isPlainObject(Object.create(Object.create(null)))).toEqual(true);
expect(z.core.util.isPlainObject([])).toEqual(false);
expect(z.core.util.isPlainObject(new Date())).toEqual(false);
expect(z.core.util.isPlainObject(null)).toEqual(false);
Expand Down
19 changes: 14 additions & 5 deletions packages/zod/src/v4/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,20 @@ export const allowsEval: { value: boolean } = cached(() => {
});

export function isPlainObject(data: any): data is Record<PropertyKey, unknown> {
return (
typeof data === "object" &&
data !== null &&
(Object.getPrototypeOf(data) === Object.prototype || Object.getPrototypeOf(data) === null)
);
if (typeof data !== "object" || data === null) return false;
if (Object.prototype.toString.call(data) !== Object.prototype.toString.call({})) return false;
let proto = Object.getPrototypeOf(data);
if (proto === null || proto === Object.prototype) {
return true;
}
while (proto !== null) {
const nextProto = Object.getPrototypeOf(proto);
if (nextProto === Object.prototype || nextProto === null) {
return true;
}
proto = nextProto;
}
return false;
}

export function numKeys(data: any): number {
Expand Down
0