diff --git a/src/utils/redact.test.ts b/src/utils/redact.test.ts index a87a23690..6e9713faf 100644 --- a/src/utils/redact.test.ts +++ b/src/utils/redact.test.ts @@ -34,3 +34,25 @@ test("no redaction of unrelated keys", () => { bar: "foo", }); }); + +test("no redaction of missing keys", () => { + expect( + redact({ foo: "bar" } as { foo: string; bar: string | undefined }, "bar"), + ).to.deep.equal({ + foo: "bar", + }); +}); + +test("no redaction of null values", () => { + expect(redact({ foo: "bar", bar: null }, "bar")).to.deep.equal({ + foo: "bar", + bar: null, + }); +}); + +test("no redaction of undefined values", () => { + expect(redact({ foo: "bar", bar: undefined }, "bar")).to.deep.equal({ + foo: "bar", + bar: undefined, + }); +}); diff --git a/src/utils/redact.ts b/src/utils/redact.ts index 7764aacb7..9a3ea9090 100644 --- a/src/utils/redact.ts +++ b/src/utils/redact.ts @@ -12,6 +12,14 @@ Please see LICENSE in the repository root for full details. * @param keys Keys to be redacted in the object * @returns A new object with the specified properties redacted */ -export function redact(obj: T, ...keys: (keyof T)[]): T { - return { ...obj, ...Object.fromEntries(keys.map((k) => [k, ""])) }; +export function redact( + obj: T, + ...keys: (keyof T)[] +): Record { + const result: Record = { ...obj }; + for (const key of keys) + if (key in result && result[key] != null) { + result[key] = ""; + } + return result; }