From e7a5edcf82325c165eeaeafaeb84f5e2d545a723 Mon Sep 17 00:00:00 2001 From: Jeremy Bull Date: Sat, 6 Nov 2021 00:11:45 +0000 Subject: [PATCH] Records should be partial so we're not required to use all keys. --- src/__tests__/record.test.ts | 22 +++++++++++++++++++--- src/types.ts | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/__tests__/record.test.ts b/src/__tests__/record.test.ts index 9d964d73f..cb3a0547a 100644 --- a/src/__tests__/record.test.ts +++ b/src/__tests__/record.test.ts @@ -17,17 +17,17 @@ const recordWithLiteralKeys = z.record( type recordWithLiteralKeys = z.infer; test("type inference", () => { - const f1: util.AssertEqual> = true; + const f1: util.AssertEqual>> = 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; }); @@ -90,6 +90,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/src/types.ts b/src/types.ts index 8c78e26c6..df5e984b7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2168,9 +2168,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;