From 1c0084f3435e54ff223cf0f29c0bdd9d895b0a0b Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Tue, 1 Mar 2022 20:20:46 -0500 Subject: [PATCH] =?UTF-8?q?chore:=20automatic=20fixes=20and=20code=20gener?= =?UTF-8?q?ation=20for=2066cbfe09=20(Merge=20pull=20reque=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/colinhacks/zod/actions/runs/1919680386 --- deno/lib/__tests__/record.test.ts | 25 ++++++++++++++++++++++--- deno/lib/types.ts | 4 ++-- src/__tests__/record.test.ts | 5 ++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/deno/lib/__tests__/record.test.ts b/deno/lib/__tests__/record.test.ts index 8ea322ff3..9f5135f2f 100644 --- a/deno/lib/__tests__/record.test.ts +++ b/deno/lib/__tests__/record.test.ts @@ -18,17 +18,20 @@ const recordWithLiteralKeys = z.record( type recordWithLiteralKeys = z.infer; test("type inference", () => { - const f1: util.AssertEqual> = true; + const f1: util.AssertEqual< + booleanRecord, + Partial> + > = true; f1; const f2: util.AssertEqual< recordWithEnumKeys, - Record<"Tuna" | "Salmon", string> + Partial> > = true; f2; const f3: util.AssertEqual< recordWithLiteralKeys, - Record<"Tuna" | "Salmon", string> + Partial> > = true; f3; }); @@ -91,6 +94,22 @@ test("key schema", () => { Salmon: "asdf", }); + // shouldn't require us to specify all props in record + const result3 = recordWithEnumKeys.parse({ + Tuna: "abcd", + }); + expect(result3).toEqual({ + Tuna: "abcd", + }); + + // shouldn't require us to specify all props in record + const result4 = recordWithLiteralKeys.parse({ + Salmon: "abcd", + }); + expect(result4).toEqual({ + Salmon: "abcd", + }); + expect(() => recordWithEnumKeys.parse({ Tuna: "asdf", diff --git a/deno/lib/types.ts b/deno/lib/types.ts index a71a8346e..93092b920 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -2367,9 +2367,9 @@ export class ZodRecord< Key extends KeySchema = ZodString, Value extends ZodTypeAny = ZodTypeAny > extends ZodType< - Record, + Partial>, ZodRecordDef, - Record + Partial> > { get keySchema() { return this._def.keyType; diff --git a/src/__tests__/record.test.ts b/src/__tests__/record.test.ts index cb3a0547a..612b4b221 100644 --- a/src/__tests__/record.test.ts +++ b/src/__tests__/record.test.ts @@ -17,7 +17,10 @@ const recordWithLiteralKeys = z.record( type recordWithLiteralKeys = z.infer; test("type inference", () => { - const f1: util.AssertEqual>> = true; + const f1: util.AssertEqual< + booleanRecord, + Partial> + > = true; f1; const f2: util.AssertEqual<