英文:
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() // <- 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(() => HobbiesDto)
hobbies: HobbiesDto[];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论