【发布时间】:2019-11-15 07:27:27
【问题描述】:
我正在用 nestJS 开发 api 和微服务, 这是我的控制器功能
@Post()
@MessagePattern({ service: TRANSACTION_SERVICE, msg: 'create' })
create( @Body() createTransactionDto: TransactionDto_create ) : Promise<Transaction>{
return this.transactionsService.create(createTransactionDto)
}
当我调用 post api 时,dto 验证工作正常,但是当我使用微服务验证调用它时不起作用,它会传递给服务而不会因错误而拒绝。 这是我的 DTO
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
export class TransactionDto_create{
@IsNotEmpty()
action: string;
// @IsString()
readonly rec_id : string;
@IsNotEmpty()
readonly data : Object;
extras : Object;
// readonly extras2 : Object;
}
当我在没有操作参数的情况下调用 api 时,它显示需要执行错误操作,但是当我使用微服务调用它时
const 模式 = { service: TRANSACTION_SERVICE, msg: 'create' }; 常量数据 = {id: '5d1de5d787db5151903c80b9', extras:{'asdf':'dsf'}};
return this.client.send<number>(pattern, data)它不会抛出错误并开始服务。 我还添加了 globalpipe 验证。
app.useGlobalPipes(new ValidationPipe({
disableErrorMessages: false, // set true to hide detailed error message
whitelist: false, // set true to strip params which are not in DTO
transform: false // set true if you want DTO to convert params to DTO class by default its false
}));
它将如何适用于 api 和微服务,因为我需要在一个地方并具有相同的功能,以便每个客户端都可以调用它。
【问题讨论】:
标签: microservices nestjs class-validator