-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨feat(catalogo): implementa middleware para validação de entrada de d…
…ados | Parte 37 - Refatora o Controller "Atualizar Categoria" para Interceptar Erros e Usar Erros HTTP Apropriados - Refatora o Controller "Deletar Categoria" para Interceptar Erros e Usar Erros HTTP Apropriados - Implementa Classe Específica para Erro HTTP "400 Bad Request" - Implanta "Middleware" Para Validação de Entrada de Dados da Rota "Inserir Categoria" - Melhora Legibilidade dos Erros de Validação da Biblioteca "Zod" - Implanta "Middleware" Para Validação de Entrada de Dados da Rota "Atualizar Categoria"
- Loading branch information
1 parent
8325c8c
commit 8263828
Showing
9 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...les/catalogo/presentation/http/middlewares/valida-input-atualizar-categoria.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { HttpErrors } from "@shared/presentation/http/http.error"; | ||
import { NextFunction, Request, Response } from "express"; | ||
import { z, ZodSchema } from "zod"; | ||
import { fromZodError } from 'zod-validation-error'; | ||
|
||
const atualizarCategoriaSchema = z.object( | ||
{ | ||
id: z.string().uuid(), | ||
nome: z.string().min(3).max(50) | ||
} | ||
).strict(); | ||
|
||
const validaInputAtualizarCategoriaMiddleware = ( | ||
request: Request, | ||
response: Response, | ||
next: NextFunction) => { | ||
try { | ||
atualizarCategoriaSchema.parse(request.body); | ||
next(); | ||
} catch (error: any) { | ||
const validationError = fromZodError(error); | ||
error = new HttpErrors.BadRequestError({message: validationError.message }); | ||
next(error); | ||
} | ||
} | ||
|
||
export { validaInputAtualizarCategoriaMiddleware as validaInputAtualizarCategoria } |
26 changes: 26 additions & 0 deletions
26
...dules/catalogo/presentation/http/middlewares/valida-input-inserir-categoria.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { HttpErrors } from "@shared/presentation/http/http.error"; | ||
import { NextFunction, Request, Response } from "express"; | ||
import { z, ZodSchema } from "zod"; | ||
import { fromZodError } from 'zod-validation-error'; | ||
|
||
const inserirCategoriaSchema = z.object( | ||
{ | ||
nome: z.string().min(3).max(50) | ||
} | ||
).strict(); | ||
|
||
const validaInputInserirCategoriaMiddleware = ( | ||
request: Request, | ||
response: Response, | ||
next: NextFunction) => { | ||
try { | ||
inserirCategoriaSchema.parse(request.body); | ||
next(); | ||
} catch (error: any) { | ||
const validationError = fromZodError(error); | ||
error = new HttpErrors.BadRequestError({message: validationError.message }); | ||
next(error); | ||
} | ||
} | ||
|
||
export { validaInputInserirCategoriaMiddleware as validaInputInserirCategoria } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters