add custom validation pipe
This commit is contained in:
parent
4667e57f0c
commit
4e2ee28219
|
@ -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),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,11 @@
|
||||||
import { Mapper } from '@automapper/core';
|
import { Mapper } from '@automapper/core';
|
||||||
import { InjectMapper } from '@automapper/nestjs';
|
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 { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||||
import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException';
|
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 { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
|
||||||
import { UserPresenter } from './user.presenter';
|
import { UserPresenter } from './user.presenter';
|
||||||
import { ICollection } from '../../../database/src/interfaces/collection.interface';
|
import { ICollection } from '../../../database/src/interfaces/collection.interface';
|
||||||
|
import { RpcValidationPipe } from './rpc.validation-pipe';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class UsersController {
|
export class UsersController {
|
||||||
|
@ -70,6 +76,12 @@ export class UsersController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UsePipes(
|
||||||
|
new RpcValidationPipe({
|
||||||
|
whitelist: true,
|
||||||
|
forbidUnknownValues: false,
|
||||||
|
}),
|
||||||
|
)
|
||||||
@GrpcMethod('UsersService', 'Update')
|
@GrpcMethod('UsersService', 'Update')
|
||||||
async updateUser(data: UpdateUserRequest): Promise<UserPresenter> {
|
async updateUser(data: UpdateUserRequest): Promise<UserPresenter> {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,24 +1,29 @@
|
||||||
import { AutoMap } from '@automapper/classes';
|
import { AutoMap } from '@automapper/classes';
|
||||||
import { IsString } from 'class-validator';
|
import { IsOptional, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class CreateUserRequest {
|
export class CreateUserRequest {
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
uuid?: string;
|
uuid?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
firstName?: string;
|
firstName?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
lastName?: string;
|
lastName?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
email?: string;
|
email?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
phone?: string;
|
phone?: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,35 @@
|
||||||
import { AutoMap } from '@automapper/classes';
|
import { AutoMap } from '@automapper/classes';
|
||||||
import { IsString } from 'class-validator';
|
import {
|
||||||
|
IsEmail,
|
||||||
|
IsNotEmpty,
|
||||||
|
IsOptional,
|
||||||
|
IsPhoneNumber,
|
||||||
|
IsString,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
export class UpdateUserRequest {
|
export class UpdateUserRequest {
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
uuid: string;
|
uuid: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
firstName?: string;
|
firstName?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
lastName?: string;
|
lastName?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsEmail()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
email?: string;
|
email?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsPhoneNumber()
|
||||||
|
@IsOptional()
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
phone?: string;
|
phone?: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue