Skip to content

Commit d42c1b2

Browse files
authored
fix(config): sanitize config failing on array with multiple problems (@fehmer) (monkeytypegame#7221)
Sanitize throws error if an object contains an array with 1) an invalid value and 2) too few items. The list of problems contains the path to the array twice and tries to remove the invalid element from the already deleted array. Config object: ```json "customPolyglot": [ "japanese" ] ```
1 parent b9dd150 commit d42c1b2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

frontend/__tests__/utils/sanitize.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ describe("sanitize function", () => {
151151
optional: { name: "Alice", age: 23 },
152152
},
153153
},
154+
{
155+
input: {
156+
name: "Alice",
157+
//results in two errors on the same path. array with invalid value and not enough items
158+
enumArray: ["invalid" as any],
159+
},
160+
expected: {
161+
mandatory: false,
162+
partial: { name: "Alice" }, //enumArray is removed
163+
optional: false,
164+
},
165+
},
154166
];
155167

156168
it.for(testCases)("object mandatory with $input", ({ input, expected }) => {

frontend/src/ts/utils/sanitize.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { z } from "zod";
22

33
function removeProblems<T extends object | unknown[]>(
4-
obj: T,
4+
obj: T | undefined,
55
problems: (number | string)[],
66
): T | undefined {
7+
//already removed
8+
if (obj === undefined) return undefined;
9+
710
if (Array.isArray(obj)) {
811
if (problems.length === obj.length) return undefined;
912

0 commit comments

Comments
 (0)