diff --git a/deno/lib/__tests__/object.test.ts b/deno/lib/__tests__/object.test.ts index e7ea349ab..fac2b9ffd 100644 --- a/deno/lib/__tests__/object.test.ts +++ b/deno/lib/__tests__/object.test.ts @@ -293,3 +293,18 @@ test("intersection of object with refine with date", async () => { const result = await schema.parseAsync({ a: new Date(1637353595983) }); expect(result).toEqual({ a: new Date(1637353595983) }); }); + +test("constructor key", () => { + const person = z + .object({ + name: z.string(), + }) + .strict(); + + expect(() => + person.parse({ + name: "bob dylan", + constructor: 61, + }) + ).toThrow(); +}); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index a9a3c0b8f..a71a8346e 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -1499,7 +1499,7 @@ export class ZodObject< const { shape, keys: shapeKeys } = this._getCached(); const dataKeys = util.objectKeys(ctx.data); - const extraKeys = dataKeys.filter((k) => !(k in shape)); + const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k)); const pairs: { key: ParseReturnType; diff --git a/package.json b/package.json index 3eca7dbfd..872839608 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zod", - "version": "3.12.1", + "version": "3.12.2", "description": "TypeScript-first schema declaration and validation library with static type inference", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/__tests__/object.test.ts b/src/__tests__/object.test.ts index 3512c3879..fc01e6397 100644 --- a/src/__tests__/object.test.ts +++ b/src/__tests__/object.test.ts @@ -292,3 +292,18 @@ test("intersection of object with refine with date", async () => { const result = await schema.parseAsync({ a: new Date(1637353595983) }); expect(result).toEqual({ a: new Date(1637353595983) }); }); + +test("constructor key", () => { + const person = z + .object({ + name: z.string(), + }) + .strict(); + + expect(() => + person.parse({ + name: "bob dylan", + constructor: 61, + }) + ).toThrow(); +}); diff --git a/src/types.ts b/src/types.ts index 6f9e8455d..4fea82f45 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1499,7 +1499,7 @@ export class ZodObject< const { shape, keys: shapeKeys } = this._getCached(); const dataKeys = util.objectKeys(ctx.data); - const extraKeys = dataKeys.filter((k) => !(k in shape)); + const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k)); const pairs: { key: ParseReturnType;