在我的API中考虑此端点:
@Post('/convert')
@UseInterceptors(FileInterceptor('image'))
convert(
@UploadedFile() image: any,
@Body(
new ValidationPipe({
validationError: {
target: false,
},
// this is set to true so the validator will return a class-based payload
transform: true,
// this is set because the validator needs a tranformed payload into a class-based
// object, otherwise nothing will be validated
transformOptions: { enableImplicitConversion: true },
}),
)
parameters: Parameters,
) {
return this.converterService.start(image, parameters);
}
请求的主体(设置为parameters
参数,包含一个称为laserMode
的属性,该属性应为布尔类型,它在parameters DTO上已得到验证):>
@IsDefined() @IsBoolean() public laserMode: boolean;
现在是奇怪的部分,当从PostMan发送请求时:
laserMode = false
laserMode = cool
(布尔值以外的字符串)[我注意到laserMode
总是始终设置为true
export class Parameters { ... constructor() { console.log('this :', this); } ... }
我没看到财产!
Note
:当从请求中删除laserMode
时,返回预期的验证错误(应定义,应为布尔值)。// the logged instance 'this' in the constructor this : Parameters { toolDiameter: 1, sensitivity: 0.95, scaleAxes: 200, deepStep: -1, whiteZ: 0, blackZ: -2, safeZ: 2, workFeedRate: 3000, idleFeedRate: 1200, laserPowerOn: 'M04', laserPowerOff: 'M05', invest: Invest { x: false, y: true } } // the logged laserMode value in the endpoint handler in the controller parameters.laserMode in controller : true // the logged laser value from the service parameters.laserMode in service : true
考虑我的API中的此端点:@Post('/ convert')@UseInterceptors(FileInterceptor('image'))convert(@UploadedFile()图片:任意,@ Body(new ValidationPipe({...
这是由于选项enableImplicitConversion
。显然,所有字符串值都被解释为true
,甚至字符串'false'
。
[有一个issue请求更改class-transformer
的行为。