如何在Swagger(NestJS,TypeScript)中描述具有计算属性的对象?

huangapple go评论68阅读模式
英文:

How to discribe object with computed properties in swagger (nestjs, typescript)?

问题

{
name: string;
age: number;
hobbies: {
running: HobbyDto,
swiming: HobbyDto,
...
}
}
需要描述该对象,应该如下所示:

{
@ApiProperty()
name: string;

@ApiProperty()
age: number;

@ApiProperty() // <- 如何装饰这个对象?
hobbies: {
[key: string]: HobbyDto;
}
}

英文:

I have some DTO as below and I need to describe the DTO for Swagger:

{
  name: string;
  age: number;
  hobbies: { // maybe set of any hobbies 
    running: HobbyDto,
    swiming: HobbyDto,
    ...
  }
}

I need to describe the object and it should be as bellow:

{
  @ApiProperty()
  name: string;

  @ApiProperty()
  age: number;

  @ApiProperty() // &lt;- how to decorate this object?
  hobbies: { 
    [key: string]: HobbyDto;
  }
}

答案1

得分: 1

我认为您应该添加另一个DTO类,并像下面这样使用嵌套验证:

@ApiProperty()
@ValidateNested({ each: true })
@Type(() => HobbiesDto)
hobbies: HobbiesDto[];
英文:

I think you should add another DTO class and use nested validate like this below:

@ApiProperty()
@ValidateNested({ each: true })
@Type(() =&gt; HobbiesDto)
hobbies: HobbiesDto[];

huangapple
  • 本文由 发表于 2023年2月27日 15:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定