.
This commit is contained in:
parent
3965c807e1
commit
417d753f1e
@ -3,8 +3,9 @@ import httpStatus from "http-status";
|
||||
import { ZodSchema } from "zod";
|
||||
import { ApiError } from "./api-error";
|
||||
|
||||
export const validateRequestDTO =
|
||||
(schema: ZodSchema) => (req: Request, res: Response, next: NextFunction) => {
|
||||
export const validateAndParseBody =
|
||||
(schema: ZodSchema, options?: { sanitize?: boolean }) =>
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
const result = schema.safeParse(req.body);
|
||||
if (!result.success) {
|
||||
// Construye errores detallados
|
||||
@ -24,6 +25,9 @@ export const validateRequestDTO =
|
||||
}
|
||||
|
||||
// Si pasa la validación, opcionalmente reescribe req.body
|
||||
if (options?.sanitize ?? true) {
|
||||
req.body = result.data;
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { validateAndParseBody } from "@common/presentation";
|
||||
import {
|
||||
ICreateAccountRequestSchema,
|
||||
IGetAccountRequestSchema,
|
||||
@ -19,7 +19,7 @@ export const accountsRouter = (appRouter: Router) => {
|
||||
|
||||
routes.get(
|
||||
"/",
|
||||
validateRequestDTO(ListAccountsRequestSchema),
|
||||
validateAndParseBody(ListAccountsRequestSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -29,7 +29,7 @@ export const accountsRouter = (appRouter: Router) => {
|
||||
|
||||
routes.get(
|
||||
"/:invoiceId",
|
||||
validateRequestDTO(IGetAccountRequestSchema),
|
||||
validateAndParseBody(IGetAccountRequestSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -39,7 +39,7 @@ export const accountsRouter = (appRouter: Router) => {
|
||||
|
||||
routes.post(
|
||||
"/",
|
||||
validateRequestDTO(ICreateAccountRequestSchema),
|
||||
validateAndParseBody(ICreateAccountRequestSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -49,7 +49,7 @@ export const accountsRouter = (appRouter: Router) => {
|
||||
|
||||
routes.put(
|
||||
"/:invoiceId",
|
||||
validateRequestDTO(IUpdateAccountRequestSchema),
|
||||
validateAndParseBody(IUpdateAccountRequestSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { validateAndParseBody } from "@common/presentation";
|
||||
import { checkTabContext, checkUser } from "@contexts/auth/infraestructure";
|
||||
import {
|
||||
buildLoginController,
|
||||
@ -29,7 +29,7 @@ export const authRouter = (appRouter: Router) => {
|
||||
*
|
||||
* @apiError (400) {String} message Error message.
|
||||
*/
|
||||
routes.post("/register", validateRequestDTO(RegisterUserSchema), (req, res, next) => {
|
||||
routes.post("/register", validateAndParseBody(RegisterUserSchema), (req, res, next) => {
|
||||
buildRegisterController().execute(req, res, next);
|
||||
});
|
||||
|
||||
@ -50,7 +50,7 @@ export const authRouter = (appRouter: Router) => {
|
||||
*/
|
||||
routes.post(
|
||||
"/login",
|
||||
validateRequestDTO(LoginUserSchema),
|
||||
validateAndParseBody(LoginUserSchema),
|
||||
checkTabContext,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
buildLoginController().execute(req, res, next);
|
||||
@ -79,7 +79,7 @@ export const authRouter = (appRouter: Router) => {
|
||||
|
||||
routes.post(
|
||||
"/refresh",
|
||||
validateRequestDTO(RefreshTokenSchema),
|
||||
validateAndParseBody(RefreshTokenSchema),
|
||||
checkTabContext,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
buildRefreshTokenController().execute(req, res, next);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { validateAndParseBody } from "@common/presentation";
|
||||
import {
|
||||
buildCreateInvoiceController,
|
||||
buildGetInvoiceController,
|
||||
@ -14,7 +14,7 @@ export const invoicesRouter = (appRouter: Router) => {
|
||||
|
||||
routes.get(
|
||||
"/",
|
||||
validateRequestDTO(IListInvoicesRequestSchema),
|
||||
validateAndParseBody(IListInvoicesRequestSchema),
|
||||
//checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -24,7 +24,7 @@ export const invoicesRouter = (appRouter: Router) => {
|
||||
|
||||
routes.get(
|
||||
"/:invoiceId",
|
||||
validateRequestDTO(IGetInvoiceRequestSchema),
|
||||
validateAndParseBody(IGetInvoiceRequestSchema),
|
||||
//checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -34,7 +34,7 @@ export const invoicesRouter = (appRouter: Router) => {
|
||||
|
||||
routes.post(
|
||||
"/",
|
||||
//validateRequestDTO(ICreateInvoiceRequestSchema),
|
||||
//validateAndParseBody(ICreateInvoiceRequestSchema),
|
||||
//checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -45,7 +45,7 @@ export const invoicesRouter = (appRouter: Router) => {
|
||||
/*
|
||||
routes.put(
|
||||
"/:invoiceId",
|
||||
validateRequestDTO(IUpdateInvoiceRequestSchema),
|
||||
validateAndParseBody(IUpdateInvoiceRequestSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
@ -55,7 +55,7 @@ export const invoicesRouter = (appRouter: Router) => {
|
||||
|
||||
routes.delete(
|
||||
"/:invoiceId",
|
||||
validateRequestDTO(IDeleteInvoiceRequestSchema),
|
||||
validateAndParseBody(IDeleteInvoiceRequestSchema),
|
||||
checkTabContext,
|
||||
//checkUser,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { validateRequestDTO } from "@common/presentation";
|
||||
import { validateAndParseBody } from "@common/presentation";
|
||||
import { checkTabContext, checkUserIsAdmin } from "@contexts/auth/infraestructure";
|
||||
import { buildListUsersController, ListUsersSchema } from "@contexts/auth/presentation";
|
||||
import { NextFunction, Request, Response, Router } from "express";
|
||||
@ -8,7 +8,7 @@ export const usersRouter = (appRouter: Router) => {
|
||||
|
||||
routes.get(
|
||||
"/",
|
||||
validateRequestDTO(ListUsersSchema),
|
||||
validateAndParseBody(ListUsersSchema),
|
||||
checkTabContext,
|
||||
checkUserIsAdmin,
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user