POST method로 body를 받을 때, Array에 Object 데이터를 전송할 수 있다.
DTO를 Object의 데이터로 구성하고 각 배열마다 유효성 검사를 하고 싶었다.
Object별 DTO를 작성한다.
import { IsInt, IsOptional, IsString, IsUrl } from "class-validator";
export class ModifyDTO{
@IsString()
readonly title: string;
@IsString()
readonly description: string;
@IsUrl()
readonly imageUrl: string;
@IsOptional()
@IsUrl()
readonly videoUrl: string;
@IsInt()
readonly priority: number;
}
컨트롤러에서 바디 데코레이터에 파이프를 달아준다.
@Post("/modify")
async updatePost(@Body(new ParseArrayPipe({items: ModifyDTO})) body:ModifyDTO[] ){
console.log(body);
return ;
}
파이프를 통해 각 배열마다 정상적으로 유효성 검사가 실행된다.
'Server > NodeJS & NestJS' 카테고리의 다른 글
NestJS) new ParseArrayPipe -> Array DTO Check (0) | 2022.09.22 |
---|---|
NestJS) sql-template-strings에서 JOIN 사용하기 (0) | 2022.09.05 |
NestJS) JSON.stringify Object DTO에서 확인하기 (0) | 2022.07.08 |
Twitter Api Oauth 1.0a / Oauth 2.0 / API v1.1 / API v2.0 (0) | 2022.07.06 |
NestJS) aws-sdk this.client.send is not a function error (0) | 2022.06.23 |