Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: add game schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonHowe committed Jul 10, 2020
1 parent 93dbe17 commit 1119ea2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/api/src/modules/games/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import { requireAuthenticated } from "../auth/middleware/requireAuthenticated";
import { createGame } from "./actions/createGame";
import { removeOldGame } from "./actions/removeOldGame";
import checkPB from "./actions/checkPB";
import { newGameBody } from "./schema/newGameBody";
import { validateSchema } from "../schema/middleware/validateSchema";

const router = new Router({ prefix: "/games" });

router.post("/newGame", requireAuthenticated(), async (ctx, next) => {
const { wpm, rawwpm, accuracy } = ctx.request.body;
const { user } = ctx.session!;
const newGame = await createGame(user, wpm, rawwpm, accuracy);
await removeOldGame(user);
await checkPB(newGame);
ctx.status = 201;
ctx.body = "Successfully created a game!";
await next();
});
router.post(
"/newGame",
requireAuthenticated(),
validateSchema(newGameBody, "body"),
async (ctx, next) => {
const { wpm, rawwpm, accuracy } = ctx.request.body;
const { user } = ctx.session!;
const newGame = await createGame(user, wpm, rawwpm, accuracy);
await removeOldGame(user);
await checkPB(newGame);
ctx.status = 201;
ctx.body = "Successfully created a game!";
await next();
}
);

export default router.routes();
7 changes: 7 additions & 0 deletions packages/api/src/modules/games/schema/newGameBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Joi from "@hapi/joi";

export const newGameBody = Joi.object({
wpm: Joi.number(),
rawwpm: Joi.number(),
accuracy: Joi.number()
});

0 comments on commit 1119ea2

Please sign in to comment.