1

我像这样在 Dto 中创建 @ApiProperty()

export class UpdateMeassageDto {

@ApiProperty()

message: {

    hash: string;


    created_at: Date;


    updated_at: Date;
}

}

然后它得到一个像这样的空结果

https://i.stack.imgur.com/kYGP5.jpg

4

2 回答 2

2

您可以通过以下方式进行

import { ApiProperty, getSchemaPath } from '@nestjs/swagger';

export class MessageDto {
    @ApiProperty({
        type: 'string',
        example: 'hx1231',
    })
    hash: string;

    @ApiProperty({
        type: 'date',
        example: '2021-08-04T19:39:00.829Z',
    })
    created_at: Date;

    @ApiProperty({
        type: 'string',
        format: 'date-time',
        example: '2021-08-04T19:39:00.829Z',
    })
    updated_at: Date;
}

export class UpdateMessageDto {
    @ApiProperty({
        type: 'array',
        items: { $ref: getSchemaPath(MessageDto) },
    })
    message: MessageDto;
}

每个属性都需要 ApiProperty。您可以使用 $ref 在另一个 DTO 中使用一个 DTO。在 DTO 中也使用类验证器会很好。

于 2021-08-04T19:44:28.593 回答
0

尝试将新类型定义为:

export class Message{

    @ApiProperty()
    hash: string;

    @ApiProperty()
    created_at: Date;

    @ApiProperty()
    updated_at: Date;
}

并按如下方式更新您的 DTO:

export class UpdateMessageDto {

    @ApiProperty()
    message: Message
}

于 2021-08-04T13:20:07.117 回答