From d52af936f6a7ea6d42d803167f49b0db0fe69c88 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Tue, 12 Aug 2025 13:51:58 +0200 Subject: [PATCH] fix(account): error while loading old filter presets (@fehmer) (#6873) fixes #6872 --- frontend/__tests__/utils/misc.spec.ts | 40 +++++++++---------- .../src/ts/elements/account/result-filters.ts | 10 ++++- frontend/src/ts/utils/misc.ts | 5 --- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/frontend/__tests__/utils/misc.spec.ts b/frontend/__tests__/utils/misc.spec.ts index 61e6b0bc3..f9b554f01 100644 --- a/frontend/__tests__/utils/misc.spec.ts +++ b/frontend/__tests__/utils/misc.spec.ts @@ -11,8 +11,6 @@ import { } from "../../src/ts/utils/strings"; import { Language } from "@monkeytype/schemas/languages"; -//todo this file is in the wrong place - describe("misc.ts", () => { describe("getLanguageDisplayString", () => { it("should return correctly formatted strings", () => { @@ -289,25 +287,6 @@ describe("misc.ts", () => { }); }); - it("should throw on nested objects", () => { - const schema = z - .object({ - name: z.string(), - info: z.object({ age: z.number() }).partial(), - }) - .partial(); - const obj = { - name: "Alice", - info: { age: "42" as any }, - }; - - expect(() => { - sanitize(schema, obj); - }).toThrowError( - "sanitize does not support nested objects yet. path: info.age" - ); - }); - it("should remove entire property if all array elements are invalid", () => { const obj = { name: "Alice", age: 30, tags: [123, 456] as any }; const sanitized = sanitize(schema, obj); @@ -328,6 +307,25 @@ describe("misc.ts", () => { expect(sanitized).not.toHaveProperty("name"); }); + it("should remove nested objects if not valid", () => { + //GIVEN + const schema = z + .object({ + name: z.string(), + info: z.object({ age: z.number() }).partial(), + }) + .partial(); + + const obj = { + name: "Alice", + info: { age: "42" as any }, + }; + //WHEN / THEN + expect(sanitize(schema, obj)).toEqual({ + name: "Alice", + }); + }); + it("should strip extra keys", () => { const obj = { name: "bob", diff --git a/frontend/src/ts/elements/account/result-filters.ts b/frontend/src/ts/elements/account/result-filters.ts index b1357beda..dc075aadc 100644 --- a/frontend/src/ts/elements/account/result-filters.ts +++ b/frontend/src/ts/elements/account/result-filters.ts @@ -56,7 +56,10 @@ const resultFiltersLS = new LocalStorageWithSchema({ return defaultResultFilters; } return mergeWithDefaultFilters( - Misc.sanitize(ResultFiltersSchema, unknown as ResultFilters) + Misc.sanitize( + ResultFiltersSchema.partial().strip(), + unknown as ResultFilters + ) ); }, }); @@ -927,7 +930,10 @@ $(".group.presetFilterButtons .filterBtns").on( function verifyResultFiltersStructure(filterIn: ResultFilters): ResultFilters { const filter = mergeWithDefaultFilters( - Misc.sanitize(ResultFiltersSchema, Misc.deepClone(filterIn)) + Misc.sanitize( + ResultFiltersSchema.partial().strip(), + Misc.deepClone(filterIn) + ) ); return filter; diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index 862cd2ba3..db833c52d 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -734,11 +734,6 @@ export function sanitize( let val = errors.get(element); if (typeof error.path[1] === "number") { val = [...(val ?? []), error.path[1]]; - } else if (error.path.length > 1) { - throw new Error( - "sanitize does not support nested objects yet. path: " + - error.path.join(".") - ); } errors.set(element, val); }