-
NestJS class-validator 사용시 일관성 있는 에러처리Nest.js 2024. 3. 8. 12:25728x90
class-vaildator Dto 중첩 객체 에러 처리 문제
{ "success": false, "timestamp": "2024-05-16T03:59:41.456Z", "status": 400, "message": "Validation failed", "path": "/api/groups/75aca3da-1dac-48ef-84b8-cdf1be8fe37d/schedules", "errors": [ { "field": "periods", "message": "값이 없습니다!", "children": [ {}, {}, { "field": "tourisms", "message": "값이 없습니다!", "children": [ {}, { "field": "endTime", "message": "값이 없습니다!" } ] } ] }, { "field": "sharedFamIds", "message": "UUID를 입력 해주세요!" } ] }Custom-Vaildation-Pipe


결과
{ "success": false, "timestamp": "2024-05-16T03:59:41.456Z", "status": 400, "message": "periods.2.tourisms.1.endTime에 값이 없습니다!,sharedFamIds에 UUID를 입력 해주세요!, "path": "/api/groups/75aca3da-1dac-48ef-84b8-cdf1be8fe37d/schedules" }완벽하진 않지만 해당 부분을 커스텀해서 좀 더 리팩토링하면 더 좋은 에러 메세지 포맷을 제공 할 수 있을것 같다.
Reference
- https://dev.to/nithinkjoy/how-to-use-class-validator-and-return-custom-error-object-in-nestjs-562h
How to use class-validator and generate custom error object in nest.js
Objective The main objective of this article is showing how to generate custom error...
dev.to
- https://stackoverflow.com/questions/60270468/throw-same-error-format-as-class-validator-in-nestjs
Throw same error format as `class-validator` in NestJS
Let's have this controller in NestJS project: @Post('resetpassword') @HttpCode(200) async requestPasswordReset( @Body() body: RequestPasswordResetDTO, ): Promise<boolean> { t...
stackoverflow.com
728x90'Nest.js' 카테고리의 다른 글
NestJS redis 라이브러리 (0) 2024.03.17 NestJS에서 에러 로그 생성 및 관리 (0) 2024.03.12 NestJS 개발시 tsconfig 설정시 strictNullChecks를 true로 하지 않을때 (0) 2024.02.24 NestJS에서 WebSockets 파트에서 Adapters (0) 2024.02.21 Nestjs yarn berry (0) 2023.12.20