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