From 4e2ee2821919b82ba8eb14bdc04d16a54fdb0675 Mon Sep 17 00:00:00 2001 From: Gsk54 Date: Fri, 23 Dec 2022 11:00:34 +0100 Subject: [PATCH] add custom validation pipe --- .../adapters/primaries/rpc.validation-pipe.ts | 14 ++++++++++++++ .../adapters/primaries/users.controller.ts | 14 +++++++++++++- .../users/domain/dtos/create-user.request.ts | 7 ++++++- .../users/domain/dtos/update-user.request.ts | 17 ++++++++++++++--- 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/modules/users/adapters/primaries/rpc.validation-pipe.ts diff --git a/src/modules/users/adapters/primaries/rpc.validation-pipe.ts b/src/modules/users/adapters/primaries/rpc.validation-pipe.ts new file mode 100644 index 0000000..f2b8c19 --- /dev/null +++ b/src/modules/users/adapters/primaries/rpc.validation-pipe.ts @@ -0,0 +1,14 @@ +import { Injectable, ValidationPipe } from '@nestjs/common'; +import { RpcException } from '@nestjs/microservices'; + +@Injectable() +export class RpcValidationPipe extends ValidationPipe { + createExceptionFactory() { + return (validationErrors = []) => { + return new RpcException({ + code: 3, + message: this.flattenValidationErrors(validationErrors), + }); + }; + } +} diff --git a/src/modules/users/adapters/primaries/users.controller.ts b/src/modules/users/adapters/primaries/users.controller.ts index 75d4e1c..2428e14 100644 --- a/src/modules/users/adapters/primaries/users.controller.ts +++ b/src/modules/users/adapters/primaries/users.controller.ts @@ -1,6 +1,11 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; -import { Controller } from '@nestjs/common'; +import { + Controller, + UsePipes, + ValidationError, + ValidationPipe, +} from '@nestjs/common'; import { CommandBus, QueryBus } from '@nestjs/cqrs'; import { GrpcMethod, RpcException } from '@nestjs/microservices'; import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException'; @@ -16,6 +21,7 @@ import { FindAllUsersQuery } from '../../queries/find-all-users.query'; import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query'; import { UserPresenter } from './user.presenter'; import { ICollection } from '../../../database/src/interfaces/collection.interface'; +import { RpcValidationPipe } from './rpc.validation-pipe'; @Controller() export class UsersController { @@ -70,6 +76,12 @@ export class UsersController { } } + @UsePipes( + new RpcValidationPipe({ + whitelist: true, + forbidUnknownValues: false, + }), + ) @GrpcMethod('UsersService', 'Update') async updateUser(data: UpdateUserRequest): Promise { try { diff --git a/src/modules/users/domain/dtos/create-user.request.ts b/src/modules/users/domain/dtos/create-user.request.ts index f060bee..6859b61 100644 --- a/src/modules/users/domain/dtos/create-user.request.ts +++ b/src/modules/users/domain/dtos/create-user.request.ts @@ -1,24 +1,29 @@ import { AutoMap } from '@automapper/classes'; -import { IsString } from 'class-validator'; +import { IsOptional, IsString } from 'class-validator'; export class CreateUserRequest { @IsString() + @IsOptional() @AutoMap() uuid?: string; @IsString() + @IsOptional() @AutoMap() firstName?: string; @IsString() + @IsOptional() @AutoMap() lastName?: string; @IsString() + @IsOptional() @AutoMap() email?: string; @IsString() + @IsOptional() @AutoMap() phone?: string; } diff --git a/src/modules/users/domain/dtos/update-user.request.ts b/src/modules/users/domain/dtos/update-user.request.ts index 0eca819..07eccaa 100644 --- a/src/modules/users/domain/dtos/update-user.request.ts +++ b/src/modules/users/domain/dtos/update-user.request.ts @@ -1,24 +1,35 @@ import { AutoMap } from '@automapper/classes'; -import { IsString } from 'class-validator'; +import { + IsEmail, + IsNotEmpty, + IsOptional, + IsPhoneNumber, + IsString, +} from 'class-validator'; export class UpdateUserRequest { @IsString() + @IsNotEmpty() @AutoMap() uuid: string; @IsString() + @IsOptional() @AutoMap() firstName?: string; @IsString() + @IsOptional() @AutoMap() lastName?: string; - @IsString() + @IsEmail() + @IsOptional() @AutoMap() email?: string; - @IsString() + @IsPhoneNumber() + @IsOptional() @AutoMap() phone?: string; }