From af4e8e4d7456812382ed15a94060cb6384bc0d32 Mon Sep 17 00:00:00 2001 From: Bilal Haidar Date: Sat, 13 Apr 2019 22:39:13 +0300 Subject: [PATCH] use validation-pipe globally for all inputs --- server/src/auth/auth.controller.ts | 3 --- server/src/main.ts | 11 ++++++++++- server/src/todo/task/task.controller.ts | 2 -- server/src/todo/todo.controller.ts | 3 --- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/server/src/auth/auth.controller.ts b/server/src/auth/auth.controller.ts index b05c2a8..62810b3 100644 --- a/server/src/auth/auth.controller.ts +++ b/server/src/auth/auth.controller.ts @@ -5,7 +5,6 @@ import { HttpException, HttpStatus, UsePipes, - ValidationPipe, Get, Req, UseGuards, @@ -23,7 +22,6 @@ export class AuthController { constructor(private readonly authService: AuthService) {} @Post('register') - @UsePipes(new ValidationPipe()) public async register( @Body() createUserDto: CreateUserDto, ): Promise { @@ -39,7 +37,6 @@ export class AuthController { } @Post('login') - @UsePipes(new ValidationPipe()) public async login(@Body() loginUserDto: LoginUserDto): Promise { return await this.authService.login(loginUserDto); } diff --git a/server/src/main.ts b/server/src/main.ts index f077d7b..36ca9c1 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -2,7 +2,7 @@ import 'dotenv/config'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -import { Logger } from '@nestjs/common'; +import { Logger, ValidationPipe } from '@nestjs/common'; import { getDbConnectionOptions, runDbMigrations } from '@shared/utils'; const port = process.env.PORT; @@ -12,6 +12,15 @@ async function bootstrap() { AppModule.forRoot(await getDbConnectionOptions(process.env.NODE_ENV)), ); + /** + * Apply validation for all inputs globally + */ + app.useGlobalPipes( + new ValidationPipe({ + whitelist: true, + }), + ); + /** * Run DB migrations */ diff --git a/server/src/todo/task/task.controller.ts b/server/src/todo/task/task.controller.ts index 042143e..20e83d6 100644 --- a/server/src/todo/task/task.controller.ts +++ b/server/src/todo/task/task.controller.ts @@ -5,7 +5,6 @@ import { Post, Body, Delete, - ValidationPipe, UsePipes, UseGuards, } from '@nestjs/common'; @@ -31,7 +30,6 @@ export class TaskController { } @Post('todo/:id') - @UsePipes(new ValidationPipe()) @UseGuards(AuthGuard()) async create( @Param('id') todo: string, diff --git a/server/src/todo/todo.controller.ts b/server/src/todo/todo.controller.ts index a540f81..29412cb 100644 --- a/server/src/todo/todo.controller.ts +++ b/server/src/todo/todo.controller.ts @@ -6,7 +6,6 @@ import { Body, Put, Delete, - ValidationPipe, UsePipes, UseGuards, Req, @@ -34,7 +33,6 @@ export class TodoController { } @Post() - @UsePipes(new ValidationPipe()) @UseGuards(AuthGuard()) async create( @Body() createTodoDto: CreateTodoDto, @@ -46,7 +44,6 @@ export class TodoController { } @Put(':id') - @UsePipes(new ValidationPipe()) @UseGuards(AuthGuard()) async update( @Param('id') id: string,