Skip to content

Commit

Permalink
Fix constructor key bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed Mar 2, 2022
1 parent 6433ec0 commit 0dfcc37
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions deno/lib/__tests__/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
2 changes: 1 addition & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
Expand Down

0 comments on commit 0dfcc37

Please sign in to comment.