From c8769c5b42b9db87f378492ff3d111ca639f2255 Mon Sep 17 00:00:00 2001 From: Rob O'Brien Date: Fri, 9 Feb 2024 13:56:17 +0000 Subject: [PATCH] Feat/update swe test (#26) * feat: add yup for form validation and some basic validation and tests * feat: remove yup and move to more custom validation * chore: update to double quotes --- swe/lib/utils/validateRegistrationForm.test.ts | 18 ++++++++++++++++++ swe/lib/utils/validateRegistrationForm.ts | 11 +++++++++++ swe/pages/api/register.ts | 12 ++++++++---- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 swe/lib/utils/validateRegistrationForm.test.ts create mode 100644 swe/lib/utils/validateRegistrationForm.ts diff --git a/swe/lib/utils/validateRegistrationForm.test.ts b/swe/lib/utils/validateRegistrationForm.test.ts new file mode 100644 index 0000000..dcb0051 --- /dev/null +++ b/swe/lib/utils/validateRegistrationForm.test.ts @@ -0,0 +1,18 @@ +import { Prisma } from '@prisma/client'; + +import validateRegistrationForm from "./validateRegistrationForm"; + +describe("validateRegistrationForm", () => { + it("validates correctly", async () => { + const mockRegistrationInput: Prisma.RegistrationCreateInput = { + firstName: 'firstName', + lastName: 'lastName', + email: 'email', + course: { connect: { id: 1 } }, + } + + const validatedData = await validateRegistrationForm(mockRegistrationInput); + + expect(validatedData).toEqual(undefined); + }); +}); diff --git a/swe/lib/utils/validateRegistrationForm.ts b/swe/lib/utils/validateRegistrationForm.ts new file mode 100644 index 0000000..23b020a --- /dev/null +++ b/swe/lib/utils/validateRegistrationForm.ts @@ -0,0 +1,11 @@ +import { Prisma } from "@prisma/client"; + +export class ValidationError extends Error {} + +export const validateRegistrationForm = async (requestBody: Prisma.RegistrationCreateInput) => { + if (!requestBody) { + throw new ValidationError("Empty request body"); + } +}; + +export default validateRegistrationForm; diff --git a/swe/pages/api/register.ts b/swe/pages/api/register.ts index 85fb612..d158f9d 100644 --- a/swe/pages/api/register.ts +++ b/swe/pages/api/register.ts @@ -1,6 +1,8 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { PrismaClient, Prisma } from "@prisma/client"; +import validateRegistrationForm from '../../lib/utils/validateRegistrationForm'; + const prisma = new PrismaClient(); export default async function handler( @@ -14,15 +16,17 @@ export default async function handler( } try { - const registration: Prisma.RegistrationCreateInput = JSON.parse(req.body); + const requestBody: Prisma.RegistrationCreateInput = req.body; + await validateRegistrationForm(requestBody); + const savedRegistration = await prisma.registration.create({ - data: registration, + data: requestBody, }); res.status(200).json(savedRegistration); } catch (err) { - console.log("error: " + String(err)); + console.log(err); res - .status(400) + .status(500) .json({ message: "Something went wrong", error: String(err) }); } };