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