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

Array of files request example #3

Closed matgott closed 3 years ago

matgott commented 3 years ago

Hi, I'am triying to validate an array of files but I don't know how the request must be made.

This is my DTO:

import { ApiProperty } from '@nestjs/swagger';
import { IsBooleanString, IsNotEmpty, IsNumberString, IsString } from 'class-validator';
import { HasMimeType, IsFiles, MemoryStoredFile } from 'nestjs-form-data';
import { BaseDto } from 'src/modules/base/dto/base-dto';

export class createDto extends BaseDto {
  @ApiProperty({ type: 'number' })
  @IsNumberString()
  @IsNotEmpty()
  readonly companyBranch;

  @ApiProperty({ type: 'number' })
  @IsNumberString()
  @IsNotEmpty()
  readonly entryUser;

  @ApiProperty({ type: 'number' })
  @IsNumberString()
  @IsNotEmpty()
  readonly sinisterType;

  @ApiProperty({ type: 'number' })
  @IsNumberString()
  @IsNotEmpty()
  readonly sinisterNumber;

  @ApiProperty({ type: 'string' })
  @IsNotEmpty()
  @IsString()
  readonly formType;

  @ApiProperty({ type: 'boolean' })
  @IsBooleanString()
  readonly isCanceled;

  @ApiProperty({ type: 'files' })
  @IsFiles({ each: true })
  @HasMimeType(['image/jpeg', 'image/png'], { each: true })
  readonly photos: MemoryStoredFile[];
}

And this is my request with postman: imagen But still asking me for an array of photos.

Thanks in advance!

dmitriy-nz commented 3 years ago

Hey! To validate an array of files, you can use the decorator @IsFile({ each: true }) or @IsFiles() IsFiles decorator does not need to pass each option

matgott commented 3 years ago

Hey! To validate an array of files, you can use the decorator @IsFile({ each: true }) or @IsFiles() IsFiles decorator does not need to pass each option

Hi dmitriy! Without "each: true" in isFiles() I receive an error imagen

matgott commented 3 years ago

With isFile({each: true}) works, maybe is a bug with isFiles().

Other thing, the data is parsed like this: imagen

Is strange, the property "photos" isn't an array, but has nested object with the other file.

dmitriy-nz commented 3 years ago

Yes you are right, I just released version 1.4.1 which fixed the problem Thank you :)

matgott commented 3 years ago

Yes you are right, I just released version 1.4.1 which fixed the problem Thank you :)

Thanks you for the quickly response. Now I don't receive that eror with isFiles but, with postman still asking me for an array of photos:

imagen

dmitriy-nz commented 3 years ago

Try like this image When sending an array as data, you must specify the index or like this image

matgott commented 3 years ago

But with isFile({each: true}) works great! But, then probably there is a bug with isFiles()

Try like this image When sending an array as data, you must specify the index or like this image

Amazing, thanks!

dmitriy-nz commented 3 years ago

Use @IsFile({each: true}) must be paired with@IsArray() decorator If you look closely, then when transferring an array of files without [] or [index] leads to the fact that there is only one file object in the field

For example DTO

export class FormDataTestDto {

  @IsFile({ each: true })
  @HasMimeType(['image/jpeg', 'image/png'], { each: true })
  photos: FileSystemStoredFile[];

}

Request image Debugger image photos is not an array These are the features of the work of the mapper from form data to object

dmitriy-nz commented 3 years ago

Maybe it needs to be documented