mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-06 15:18:48 +00:00
29 lines
891 B
TypeScript
29 lines
891 B
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { z, ZodError } from "zod";
|
|
|
|
import { StatusCodes } from "http-status-codes";
|
|
|
|
export function validateData(schema: z.ZodObject<any, any>) {
|
|
return (req: Request, res: Response, next: NextFunction) => {
|
|
try {
|
|
schema.parse(req.body);
|
|
next();
|
|
} catch (error) {
|
|
if (error instanceof ZodError) {
|
|
const errorMessages = error.errors.map((issue: any) => ({
|
|
message: `${issue.path.join(".")} is ${issue.message}`,
|
|
}));
|
|
res
|
|
.status(StatusCodes.BAD_REQUEST)
|
|
.json({ error: "Invalid data", details: errorMessages });
|
|
return;
|
|
} else {
|
|
res
|
|
.status(StatusCodes.INTERNAL_SERVER_ERROR)
|
|
.json({ error: "Internal Server Error" });
|
|
console.error("Unexpected error:", error);
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
}
|