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] 유효성체크 with class-validator, 파이프 본문

JS/Nest.js

[Nest JS] 유효성체크 with class-validator, 파이프

Mini_96 2024. 8. 18. 13:06

* dto 에서 사용

import { IsString, Matches, MaxLength, MinLength } from "class-validator";

export class AuthCredentialsDto{
  @IsString()
  @MinLength(4)
  @MaxLength(20)
  username : string;

  @IsString()
  @MinLength(4)
  @MaxLength(20)
  @Matches(/^[a-zA-Z0-9]*$/,{message : 'password only accepts english and number'});
  password : string;
}

 

* 컨트롤러에 파이프 넣어줘야 제대로 작동함

@Post('/signup')
signUp(@Body(ValidationPipe) authCredentialsDto : AuthCredentialsDto){
  return this.authService.signUp(authCredentialsDto);
}

결과