Skip to content

Commit

Permalink
Merge pull request #752 from Meraki-Solutions/partial-records
Browse files Browse the repository at this point in the history
Records should be partial so we're not required to use all keys
  • Loading branch information
colinhacks authored Mar 2, 2022
2 parents 7d265f6 + e7a5edc commit 66cbfe0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ const recordWithLiteralKeys = z.record(
type recordWithLiteralKeys = z.infer<typeof recordWithLiteralKeys>;

test("type inference", () => {
const f1: util.AssertEqual<booleanRecord, Record<string, boolean>> = true;
const f1: util.AssertEqual<booleanRecord, Partial<Record<string, boolean>>> = true;
f1;

const f2: util.AssertEqual<
recordWithEnumKeys,
Record<"Tuna" | "Salmon", string>
Partial<Record<"Tuna" | "Salmon", string>>
> = true;
f2;
const f3: util.AssertEqual<
recordWithLiteralKeys,
Record<"Tuna" | "Salmon", string>
Partial<Record<"Tuna" | "Salmon", string>>
> = true;
f3;
});
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2367,9 +2367,9 @@ export class ZodRecord<
Key extends KeySchema = ZodString,
Value extends ZodTypeAny = ZodTypeAny
> extends ZodType<
Record<Key["_output"], Value["_output"]>,
Partial<Record<Key["_output"], Value["_output"]>>,
ZodRecordDef<Key, Value>,
Record<Key["_input"], Value["_input"]>
Partial<Record<Key["_input"], Value["_input"]>>
> {
get keySchema() {
return this._def.keyType;
Expand Down

0 comments on commit 66cbfe0

Please sign in to comment.