mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-18 11:36:13 +08:00
fix(account): error while loading old filter presets (@fehmer) (#6873)
fixes #6872
This commit is contained in:
parent
36556c61b6
commit
d52af936f6
3 changed files with 27 additions and 28 deletions
|
@ -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",
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue