Skip to content

Commit

Permalink
- Resolved [Issue 55](#55)
Browse files Browse the repository at this point in the history
- Added test cases for `Exposed` decorated fields
  • Loading branch information
dmitriy-nz committed Jun 23, 2024
1 parent 58e733f commit 7ed5f93
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
### v1.9.91
- Resolved [Issue 55](https://github.com/dmitriy-nz/nestjs-form-data/issues/55)
- Added test cases for `Exposed` decorated fields

### v1.9.9
- Resolved [Issue 60](https://github.com/dmitriy-nz/nestjs-form-data/issues/60)
- Added test cases for `enableImplicitConversion` field in the `class-validator` transform options
- Modified `IsFile` validator to handle `enableImplicitConversion` param
- Some other test cases were improved


### v1.9.8
- Updated `README.md`, clarified `class-validator` pipe configuration

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-form-data",
"version": "1.9.9",
"version": "1.9.91",
"description": "NestJS middleware for handling multipart/form-data, which is primarily used for uploading files",
"main": "dist/index",
"types": "dist/index",
Expand Down
8 changes: 6 additions & 2 deletions src/decorators/validation/is-file.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ export function IsFile(validationOptions?: ValidationOptions): PropertyDecorator

if(validationOptions?.each){
return applyDecorators(
// Force transform to an array of files then we have each mode and only one file in the field
Transform((params: TransformFnParams) => {
if(!Array.isArray(params.value)){
return [ params.value ];
// If value isn't array and isn't empty, arraying it
// We can get empty value, do nothing in this case
if(!Array.isArray(params.value) && params.value){
return [ params.value ]
}

return params.value;
}),
transformEnableImplicitConversion,
Expand Down
56 changes: 56 additions & 0 deletions test/exposed-optional-upload.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { INestApplication } from '@nestjs/common';
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import * as request from 'supertest';
import { createTestModule } from './helpers/create-test-module';


// https://github.com/dmitriy-nz/nestjs-form-data/issues/55
describe('Express - Exposed optional upload', () => {
let app: INestApplication;

beforeEach(async () => {
app = await createTestModule();
});

it('Upload optional exposed field single file', () => {
return request
.default(app.getHttpServer())
.post('/optional-exposed-single')
.expect(200)
.expect({});
});

it('Upload optional exposed field array files', () => {
return request
.default(app.getHttpServer())
.post('/optional-exposed-array')
.expect(200)
.expect({});
});
});

describe('Fastify - Exposed optional upload', () => {
let app: NestFastifyApplication;

beforeEach(async () => {
app = (await createTestModule({
fastify: true,
})) as NestFastifyApplication;
});

it('Upload optional exposed field single file', () => {
return request
.default(app.getHttpServer())
.post('/optional-exposed-single')
.expect(200)
.expect({});
});

it('Upload optional exposed field array files', () => {
return request
.default(app.getHttpServer())
.post('/optional-exposed-array')
.expect(200)
.expect({});
});
});
18 changes: 18 additions & 0 deletions test/test-module/controllers/test.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { MimeTypeValidatorDto } from '../dto/MimeTypeValidator.dto';
import { UploadOptionalFileDto } from '../dto/UploadOptionalFile.dto';
import { CustomErrorSingleDto } from '../dto/CustomErrorSingle.dto';
import { CustomErrorArrayDto } from '../dto/CustomErrorArray.dto';
import { UploadOptionalExposedFieldSingleFileDto } from '../dto/UploadOptionalExposedFieldSingleFile.dto';
import { UploadOptionalExposedFieldArrayFileDto } from '../dto/UploadOptionalExposedFieldArrayFile.dto';

@Controller('')
export class TestController {
Expand Down Expand Up @@ -99,6 +101,22 @@ export class TestController {
return dto;
}

@Post('optional-exposed-single')
@UsePipes(new ValidationPipe({transform: true}))
@FormDataRequest()
@HttpCode(HttpStatus.OK)
optionalExposedFieldSingleFile(@Body() dto: UploadOptionalExposedFieldSingleFileDto) {
return dto;
}

@Post('optional-exposed-array')
@UsePipes(new ValidationPipe({transform: true}))
@FormDataRequest()
@HttpCode(HttpStatus.OK)
optionalExposedFieldArrayFiles(@Body() dto: UploadOptionalExposedFieldArrayFileDto) {
return dto;
}

@Post('custom-error-single')
@UsePipes(new ValidationPipe({transform: true}))
@FormDataRequest()
Expand Down
16 changes: 16 additions & 0 deletions test/test-module/dto/UploadOptionalExposedFieldArrayFile.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MemoryStoredFile } from '../../../src/classes/storage';
import { HasMimeType, IsFiles, MaxFileSize, MinFileSize } from '../../../src/decorators';
import { IsOptional } from 'class-validator';
import { Expose } from 'class-transformer';

export class UploadOptionalExposedFieldArrayFileDto {

@Expose()
@IsFiles()
@HasMimeType(['text/plain'], { each: true })
@MaxFileSize(5, { each: true })
@MinFileSize(3, { each: true })
@IsOptional()
file?: MemoryStoredFile[];

}
16 changes: 16 additions & 0 deletions test/test-module/dto/UploadOptionalExposedFieldSingleFile.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MemoryStoredFile } from '../../../src/classes/storage';
import { HasMimeType, IsFile, MaxFileSize, MinFileSize } from '../../../src/decorators';
import { IsOptional } from 'class-validator';
import { Expose } from 'class-transformer';

export class UploadOptionalExposedFieldSingleFileDto {

@Expose()
@IsFile()
@HasMimeType(['text/plain'])
@MaxFileSize(5)
@MinFileSize(3)
@IsOptional()
file?: MemoryStoredFile;

}

0 comments on commit 7ed5f93

Please sign in to comment.