我们如何在DTO中使用对象数组和nestjs中的multipart-formdata?
Posted
技术标签:
【中文标题】我们如何在DTO中使用对象数组和nestjs中的multipart-formdata?【英文标题】:How we can use array of object in DTO with multipart-formdata in nestjs? 【发布时间】:2021-07-01 02:46:17 【问题描述】:如果我使用 multipart-formdata 并且 DTO 使用包含对象数组的字段,则不允许 formdata 在 swagger DTO 中包含该字段。现在我必须使用邮递员提供的原始 JSON 选项,但我需要来自 nestjs 内置 swagger DTO 的相同实现。
请在下面找到我的控制器代码 -
@Post('/create')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiOkResponse()
@HttpCode(HttpStatus.OK)
@ApiOperation( title: 'Create a project.' )
@ApiConsumes('multipart/form-data')
@UseInterceptors(FileInterceptor('file'))
@ApiImplicitFile( name: 'image', required: true )
@ApiCreatedResponse()
async create(@Req() request: Request, @UploadedFile() image: Express.Multer.File,)
const jsonRequest = request.body;
if (request['user'])
jsonRequest.createdBy = request['user']._id;
jsonRequest.updatedBy = request['user']._id;
const result = await this.projectsService.createProject(jsonRequest, image.buffer,
image.originalname, image.mimetype, image.size);
return result;
请找到我招摇的 DTO
import ApiModelProperty from '@nestjs/swagger';
import IsString, MinLength, MaxLength, IsBoolean, IsArray from 'class-validator';
export class CreateProjectDto
// template Id
@ApiModelProperty(
example: '604b3b7d45701c85711a9f5d',
description: 'The template Id of the project.',
format: 'string',
required: false
)
readonly templateId: string;
// organization Id
@ApiModelProperty(
example: '604f73e11f43762778292b81',
description: 'The organization Id of the project.',
format: 'string',
)
readonly organizationId: string;
// name
@IsString()
@MinLength(3)
@MaxLength(255)
@ApiModelProperty(
example: 'blender box',
description: 'The name of the project.',
format: 'string',
)
readonly name: string;
// image
@ApiModelProperty(
example: 'default.png',
description: 'The image of the project.',
format: 'string',
required: false
)
readonly image: string;
// description
@IsString()
@ApiModelProperty(
example: 'test data',
description: 'The description of the project.',
format: 'string',
required: false
)
readonly description: string;
// Allow Participants to see responses
@IsString()
@ApiModelProperty(
example: "before/after",
description: 'Allow Participants to see responses from other participants.',
format: 'string',
required: false
)
readonly respondToQues: String;
// Add Participants Additional Fields
@IsArray()
@ApiModelProperty(
example: [
"fieldName": "Fav Color",
"fieldType": "checkbox",
"optionName": [
"option1",
"option2",
"option3",
"option4"
]
,
"fieldName": "Meal Preference",
"fieldType": "radio button",
"optionName": [
"option1",
"option2",
"option3",
"option4"
]
,
"fieldName": "Org Name",
"fieldType": "dropdown",
"optionName": [
"option1",
"option2",
"option3",
"option4"
]
,
"fieldName": "Bio",
"fieldType": "single-line text",
"optionName": ""
,
"fieldName": "Tagline",
"fieldType": "multi-line text",
"optionName": ""
],
description: 'Additional meta fields that can be attached to each participant within the project.',
format: 'string',
required: false
)
readonly additionalFields: string
// createdBy
createdBy: string;
// updatedBy
updatedBy: string;
上面的 DTO 文件具有导致上述 multipart-formdata 问题的附加字段选项。
【问题讨论】:
【参考方案1】:当属性为数组时,必须手动指明数组类型,如下图:
@ApiProperty( type: [String] )
names: string[];
要么包含类型作为数组的第一个元素(如上所示),要么将 isArray 属性设置为 true。
【讨论】:
我对数组类型做同样的事情,但我的问题是 multipart/formdata 不接受我上面提到的对象字段数组。以上是关于我们如何在DTO中使用对象数组和nestjs中的multipart-formdata?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 NestJs 框架中使用 postgres 和 sequelize 正确定义 DTO 对象