dmitriy-nz / nestjs-form-data

NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
https://www.npmjs.com/package/nestjs-form-data
MIT License
118 stars 23 forks source link

Nested types #8

Closed unkindypie closed 3 years ago

unkindypie commented 3 years ago

Feature request: add support for nested DTOs/arrays of nested DTOs.

For example, we have this DTO:

 class RegisterUserDto {
  @ApiProperty()
  @IsEmail()
  email: string;

  @ApiProperty()
  @IsString()
  firstName: string;

  @ApiProperty()
  @IsString()
  lastName: string;

  @ApiProperty({ nullable: true })
  @IsString({ each: true })
  expertiseKeywords?: string[];

  @ApiProperty({ type: [CreateCredentialDocument], nullable: true })
  @ValidateNested({ each: true })
  @Type(() => CreateCredentialDocument)
  credentialDocuments?: CreateCredentialDocument[];
}

Then we will try to call route that takes this DTO with the following from data:

image

Server will get this object:

{ email: 'qwdwd@qwdq.com', firstName: 'eqwdfewf', lastName: 'qwdwedf', birthDate: '2021-06-28T14:41:28.251Z', bio: 'ewfewf', location: 'wefewfewf', jobTitle: 'wefweewf', linkedInProfileUrl: 'qdqwdqwdqwd', expertiseKeywords: [ 'lalala' ], 'credentialDocuments[0].documentTypeId': '1' }

As you can see, array of primitives works correctly while array of objects just creates string keys.

dmitriy-nz commented 3 years ago

Hi, the problem is that the request you are sending via postman is not correct.
Use credentialDocuments[0][documentTypeId] instead of credentialDocuments[0].documentTypeId

unkindypie commented 3 years ago

It works, thank you. I should have tried it.