gpt4 book ai didi

typescript - 请求正文中的 bool 参数在 NestJS api 中始终为真

转载 作者:行者123 更新时间:2023-12-04 12:08:06 30 4
gpt4 key购买 nike

在我的 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 的属性这应该是一个 bool 类型,它在 上像这样被验证参数 DTO :

  @IsDefined()
@IsBoolean()
public laserMode: boolean;


现在是奇怪的部分,当从 PostMan 发送请求时:
  • laserMode = false
  • laserMode = cool ( bool 值以外的字符串)

  • 我注意到 laserMode始终设置为 true 这是在验证过程完成之后,因为当我在类的构造函数中 console.log Parameter 的实例时

    export class Parameters {
    ...
    constructor() {
    console.log('this :', this);
    }
    ...
    }

    没看到属性啊!

    Note: when laserMode is removed from the request, the expected validation errors are returned (should be defined, should be boolean value).



    // 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
  • 检查拼写错误
  • 使用 Vue 应用程序而不是 postman 时会注意到相同的结果。
    所以!!?
  • 最佳答案

    这就是我在设法保持 bool 类型输入的同时解决问题的方法。
    通过键引用原始对象而不是使用解构的值。

    import { Transform } from 'class-transformer';

    const ToBoolean = () => {
    const toPlain = Transform(
    ({ value }) => {
    return value;
    },
    {
    toPlainOnly: true,
    }
    );
    const toClass = (target: any, key: string) => {
    return Transform(
    ({ obj }) => {
    return valueToBoolean(obj[key]);
    },
    {
    toClassOnly: true,
    }
    )(target, key);
    };
    return function (target: any, key: string) {
    toPlain(target, key);
    toClass(target, key);
    };
    };

    const valueToBoolean = (value: any) => {
    if (value === null || value === undefined) {
    return undefined;
    }
    if (typeof value === 'boolean') {
    return value;
    }
    if (['true', 'on', 'yes', '1'].includes(value.toLowerCase())) {
    return true;
    }
    if (['false', 'off', 'no', '0'].includes(value.toLowerCase())) {
    return false;
    }
    return undefined;
    };

    export { ToBoolean };
    export class SomeClass {
    @ToBoolean()
    isSomething : boolean;
    }

    关于typescript - 请求正文中的 bool 参数在 NestJS api 中始终为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046629/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com