Feat: Update user validation schema to include username/email check

The validation schema will throw an error if the username or email is already found in the database.
This commit is contained in:
Aaron William Po
2023-05-11 22:21:49 -04:00
parent 2eb2626d54
commit 9574bbaeb9
9 changed files with 226 additions and 59 deletions

View File

@@ -3,6 +3,11 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import { z } from 'zod';
interface ValidateRequestArgs {
bodySchema?: z.ZodSchema<any>;
querySchema?: z.ZodSchema<any>;
}
/**
* Middleware to validate the request body and/or query against a zod schema.
*
@@ -18,15 +23,8 @@ import { z } from 'zod';
* @param args.querySchema The query schema to validate against.
* @throws ServerError with status code 400 if the request body or query is invalid.
*/
const validateRequest =
({
bodySchema,
querySchema,
}: {
bodySchema?: z.ZodSchema<any>;
querySchema?: z.ZodSchema<any>;
}) =>
async (req: NextApiRequest, res: NextApiResponse, next: NextHandler) => {
const validateRequest = ({ bodySchema, querySchema }: ValidateRequestArgs) => {
return (req: NextApiRequest, res: NextApiResponse, next: NextHandler) => {
if (bodySchema) {
const parsed = bodySchema.safeParse(JSON.parse(JSON.stringify(req.body)));
if (!parsed.success) {
@@ -42,8 +40,8 @@ const validateRequest =
}
req.query = parsed.data;
}
return next();
};
};
export default validateRequest;