WEBBOOK CHAPTER

NestJS 11, 시작부터 런칭까지: 29장. DTO는 외부 계약이고 TypeScript는 아니다

29장. DTO는 외부 계약이고 TypeScript는 아니다

TypeScript interface는 runtime에 사라진다. global ValidationPipe는 whitelist와 unknown field 거부, 명시적 transform 정책을 적용한다. 암묵적 숫자 변환이 빈 문자열과 큰 수를 어떻게 처리하는지 test한다. nested array는 각 element의 class와 최대 개수를 검증하며 body size도 proxy와 app 양쪽에 둔다.

create, update, response DTO를 entity 하나에서 파생해 모든 필드를 노출하지 않는다. technician의 전화·내부 score·tenant id가 response에 섞이지 않게 serializer와 contract test를 둔다. patch에서 undefined, null, 미제공의 의미를 정하고 mass assignment를 막는다.

validation error는 property path, 안정적인 code, 사용자 메시지, correlation id를 Problem Details로 변환한다. 입력 원문과 decorator 내부 message를 log에 그대로 남기지 않는다. OpenAPI schema와 실제 pipe behavior가 같은지 E2E에서 invalid fixture로 검증한다.


app.useGlobalPipes(new ValidationPipe({
  whitelist: true,
  forbidNonWhitelisted: true,
  transform: true,
  transformOptions: { enableImplicitConversion: false },
}))