NestJS ValidationPipe

Ena JJJ·2023년 6월 14일
0
npm i class-validator class-transformer

위의 명령을 통해서 class-validator, class-transformer를 설치한다 이를 통해서 매우 편리한 기능을 이용할 수 있다

import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
    }),
  );
  await app.listen(3000);
}
bootstrap();

ValidationPipe 기능을 통해서

  • whitelist : @IsString()과 같은 데코레이터가 설정된 값에 다른 값이 들어올 때, 접근조차 안하게 막아준다

  • forbidNonWhiteListed : dto에 설정된 값이 아닌 다른 값의 접근을 막는다

  • transform: url에 파라미터로 값을 넘길 때, 무조건 String으로 들어오지만 transform을 이용하면 number type, string type구분을 해준다! AweSome!!

0개의 댓글