Skip to content

Commit

Permalink
Feat/update swe test (#26)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rob1256 authored Feb 9, 2024
1 parent fd3482b commit c8769c5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 18 additions & 0 deletions swe/lib/utils/validateRegistrationForm.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
11 changes: 11 additions & 0 deletions swe/lib/utils/validateRegistrationForm.ts
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 8 additions & 4 deletions swe/pages/api/register.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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) });
}
};

0 comments on commit c8769c5

Please sign in to comment.