fix: sanitise Zod validation error messages in game controller
This commit is contained in:
parent
c081e632cf
commit
6eaf282651
2 changed files with 13 additions and 2 deletions
|
|
@ -119,6 +119,17 @@ describe("POST /api/v1/game/start", () => {
|
||||||
expect(res.status).toBe(404);
|
expect(res.status).toBe(404);
|
||||||
expect(body.success).toBe(false);
|
expect(body.success).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns a sanitised error message when the body is invalid", async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post("/api/v1/game/start")
|
||||||
|
.send({ ...validBody, difficulty: "impossible" });
|
||||||
|
const body = res.body as ErrorResponse;
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(body.error).toBe("Invalid game settings");
|
||||||
|
expect(body.error).not.toContain("Invalid literal value");
|
||||||
|
expect(body.error).not.toContain("Invalid enum value");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("POST /api/v1/game/answer", () => {
|
describe("POST /api/v1/game/answer", () => {
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ export const createGameController = (store: GameSessionStore) => ({
|
||||||
try {
|
try {
|
||||||
const gameSettings = GameRequestSchema.safeParse(req.body);
|
const gameSettings = GameRequestSchema.safeParse(req.body);
|
||||||
if (!gameSettings.success) {
|
if (!gameSettings.success) {
|
||||||
throw new ValidationError(gameSettings.error.message);
|
throw new ValidationError("Invalid game settings");
|
||||||
}
|
}
|
||||||
const gameQuestions = await createGameSession(
|
const gameQuestions = await createGameSession(
|
||||||
gameSettings.data,
|
gameSettings.data,
|
||||||
|
|
@ -35,7 +35,7 @@ export const createGameController = (store: GameSessionStore) => ({
|
||||||
try {
|
try {
|
||||||
const submission = AnswerSubmissionSchema.safeParse(req.body);
|
const submission = AnswerSubmissionSchema.safeParse(req.body);
|
||||||
if (!submission.success) {
|
if (!submission.success) {
|
||||||
throw new ValidationError(submission.error.message);
|
throw new ValidationError("Invalid answer submission");
|
||||||
}
|
}
|
||||||
const result = await evaluateAnswer(
|
const result = await evaluateAnswer(
|
||||||
submission.data,
|
submission.data,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue