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);
}

결과