Notice
Recent Posts
Recent Comments
Link
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

Mini

[Nest JS] 파이프 for 유효성체크 본문

JS/Nest.js

[Nest JS] 파이프 for 유효성체크

Mini_96 2024. 8. 17. 19:51

 

 

 

 

 

* 파이프 실습

npm install class-validator class-transformer --save

 

 

* dto 변수위에 @IsNotEmpty 등 달아주기

import { IsNotEmpty } from "class-validator";

export class CreateBoardDto{
  @IsNotEmpty()
  title:string;
  
  @IsNotEmpty()
  description:string;
}

 

* 컨트롤러에서 @UsePipe 달아주기

//생성은 post
@Post()
@UsePipes(ValidationPipe)
createBoard(    @Body() createBoardDto : CreateBoardDto) : Board{
  return this.boardsService.createBoard(createBoardDto);
}

결과

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ