Spaces:
Running
Running
| import { FastifyRequest, FastifyReply } from "fastify"; | |
| import { ZodSchema, ZodError } from "zod"; | |
| export function validateBody<T>(schema: ZodSchema<T>) { | |
| return async (request: FastifyRequest, reply: FastifyReply) => { | |
| try { | |
| request.body = schema.parse(request.body); | |
| } catch (error) { | |
| if (error instanceof ZodError) { | |
| return reply.status(400).send({ | |
| success: false, | |
| error: "Validation Error", | |
| details: error.errors.map(e => ({ | |
| field: e.path.join("."), | |
| message: e.message, | |
| })), | |
| }); | |
| } | |
| throw error; | |
| } | |
| }; | |
| } | |
| export function validateQuery<T>(schema: ZodSchema<T>) { | |
| return async (request: FastifyRequest, reply: FastifyReply) => { | |
| try { | |
| request.query = schema.parse(request.query); | |
| } catch (error) { | |
| if (error instanceof ZodError) { | |
| return reply.status(400).send({ | |
| success: false, | |
| error: "Validation Error", | |
| details: error.errors.map(e => ({ | |
| field: e.path.join("."), | |
| message: e.message, | |
| })), | |
| }); | |
| } | |
| throw error; | |
| } | |
| }; | |
| } | |
| export function validateParams<T>(schema: ZodSchema<T>) { | |
| return async (request: FastifyRequest, reply: FastifyReply) => { | |
| try { | |
| request.params = schema.parse(request.params); | |
| } catch (error) { | |
| if (error instanceof ZodError) { | |
| return reply.status(400).send({ | |
| success: false, | |
| error: "Validation Error", | |
| details: error.errors.map(e => ({ | |
| field: e.path.join("."), | |
| message: e.message, | |
| })), | |
| }); | |
| } | |
| throw error; | |
| } | |
| }; | |
| } | |