我可以在nestJS的Typescript接口上使用验证修饰符吗?

时间:2018-12-28 23:01:11

标签: javascript node.js typescript nestjs

根据我的研究,我们不能在Interfaces中使用验证器,我的问题是我是否在一个类型中嵌套属性(例如,代码片段中给出的request body属性),如何在接口上使用类属性验证器? / p>

switch bmi {
case 25.nextUp...:
    return "Your BMI is \(bmi). You are overweight"
case ..<18.5:
    return "Your BMI is \(bmi). You are underweight"
default:
    return "Your BMI is \(bmi). You are at a normal weight"
}

我用类替换了接口,但是不起作用

// types.ts
// ---------------

import {  IsInt } from 'class-validator';


export interface  IRequestDto{
    readonly headers: IHeader;
    readonly body: IBody;
}

export interface IBody{
    @IsInt()
    readonly id: number;
    readonly profId: string;
}

export interface IHeader{
    readonly ContentType: string;
    readonly consumerid: string;
}



//testController.ts
// ---------------

@Controller('test')
export class TestController {
  constructor(private testService : TestService) {}

  @Post('/createProf')
  @UsePipes(new ValidationPipe())
  async createReq(@Req() request : IRequestDto) {
    const { headers, body } = request
    let localDto = new PoidBuilder()
      .setId(body.id)
      .setConsumerId(headers.consumerid)
      .setProfId(request.body.profId)
      .build()
      
      return await this.testService.createProfile(localDto)

  }

0 个答案:

没有答案