fix(account): error while loading old filter presets (@fehmer) (#6873)

fixes #6872
This commit is contained in:
Christian Fehmer 2025-08-12 13:51:58 +02:00 committed by GitHub
parent 36556c61b6
commit d52af936f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 28 deletions

View file

@ -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",

View file

@ -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;

View file

@ -734,11 +734,6 @@ export function sanitize<T extends z.ZodTypeAny>(
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);
}