Skip to content

Commit

Permalink
use validation-pipe globally for all inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaidar committed Apr 13, 2019
1 parent a0d6a89 commit af4e8e4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
3 changes: 0 additions & 3 deletions server/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
HttpException,
HttpStatus,
UsePipes,
ValidationPipe,
Get,
Req,
UseGuards,
Expand All @@ -23,7 +22,6 @@ export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('register')
@UsePipes(new ValidationPipe())
public async register(
@Body() createUserDto: CreateUserDto,
): Promise<RegistrationStatus> {
Expand All @@ -39,7 +37,6 @@ export class AuthController {
}

@Post('login')
@UsePipes(new ValidationPipe())
public async login(@Body() loginUserDto: LoginUserDto): Promise<LoginStatus> {
return await this.authService.login(loginUserDto);
}
Expand Down
11 changes: 10 additions & 1 deletion server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down
2 changes: 0 additions & 2 deletions server/src/todo/task/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Post,
Body,
Delete,
ValidationPipe,
UsePipes,
UseGuards,
} from '@nestjs/common';
Expand All @@ -31,7 +30,6 @@ export class TaskController {
}

@Post('todo/:id')
@UsePipes(new ValidationPipe())
@UseGuards(AuthGuard())
async create(
@Param('id') todo: string,
Expand Down
3 changes: 0 additions & 3 deletions server/src/todo/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Body,
Put,
Delete,
ValidationPipe,
UsePipes,
UseGuards,
Req,
Expand Down Expand Up @@ -34,7 +33,6 @@ export class TodoController {
}

@Post()
@UsePipes(new ValidationPipe())
@UseGuards(AuthGuard())
async create(
@Body() createTodoDto: CreateTodoDto,
Expand All @@ -46,7 +44,6 @@ export class TodoController {
}

@Put(':id')
@UsePipes(new ValidationPipe())
@UseGuards(AuthGuard())
async update(
@Param('id') id: string,
Expand Down

0 comments on commit af4e8e4

Please sign in to comment.