Skip to content

Commit 3b17e9e

Browse files
committed
test: make sure sanitize throws a readable error
1 parent 0535652 commit 3b17e9e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

frontend/__tests__/utils/misc.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ describe("misc.ts", () => {
236236
name: z.string(),
237237
age: z.number().positive(),
238238
tags: z.array(z.string()),
239+
enumArray: z.array(z.enum(["one", "two"])).min(2),
239240
})
240241
.partial()
241242
.strip();
@@ -306,5 +307,15 @@ describe("misc.ts", () => {
306307
const stripped = sanitize(schema.strip(), obj);
307308
expect(stripped).not.toHaveProperty("powerLevel");
308309
});
310+
it("should provide a readable error message", () => {
311+
const obj = {
312+
arrayOneTwo: ["one", "nonexistent"],
313+
} as any;
314+
expect(() => {
315+
sanitize(schema.strip(), obj);
316+
}).toThrowError(
317+
"unable to sanitize: arrayOneTwo: Array must contain at least 2 element(s)"
318+
);
319+
});
309320
});
310321
});

frontend/src/ts/utils/misc.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,11 @@ export function sanitize<T extends z.ZodTypeAny>(
770770
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
771771
return cleanValidate.data;
772772
}
773-
throw new Error(
774-
"unable to sanitize: " + cleanValidate.error.errors.join(",")
775-
);
773+
774+
const errorsString = cleanValidate.error.errors
775+
.map((e) => e.path.join(".") + ": " + e.message)
776+
.join(", ");
777+
throw new Error("unable to sanitize: " + errorsString);
776778
}
777779

778780
export function triggerResize(): void {

0 commit comments

Comments
 (0)