Skip to content

Commit 58e733f

Browse files
committed
- Resolved [Issue 60](#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
1 parent 7dec6bf commit 58e733f

File tree

9 files changed

+157
-12
lines changed

9 files changed

+157
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# compiled output
2+
.npmrc
23
/dist
34
/node_modules
45

@@ -31,4 +32,4 @@ lerna-debug.log*
3132
!.vscode/settings.json
3233
!.vscode/tasks.json
3334
!.vscode/launch.json
34-
!.vscode/extensions.json
35+
!.vscode/extensions.json

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.npmrc

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### v1.9.9
2+
- Resolved [Issue 60](https://github.com/dmitriy-nz/nestjs-form-data/issues/60)
3+
- Added test cases for `enableImplicitConversion` field in the `class-validator` transform options
4+
- Modified `IsFile` validator to handle `enableImplicitConversion` param
5+
- Some other test cases were improved
6+
7+
18
### v1.9.8
29
- Updated `README.md`, clarified `class-validator` pipe configuration
310

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-form-data",
3-
"version": "1.9.7",
3+
"version": "1.9.9",
44
"description": "NestJS middleware for handling multipart/form-data, which is primarily used for uploading files",
55
"main": "dist/index",
66
"types": "dist/index",

src/decorators/validation/is-file.validator.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export function IsFile(validationOptions?: ValidationOptions): PropertyDecorator
2525
},
2626
}, validationOptions);
2727

28+
const transformEnableImplicitConversion = Transform(params => {
29+
return params.obj[params.key]
30+
})
31+
32+
33+
2834
if(validationOptions?.each){
2935
return applyDecorators(
3036
Transform((params: TransformFnParams) => {
@@ -33,12 +39,15 @@ export function IsFile(validationOptions?: ValidationOptions): PropertyDecorator
3339
}
3440
return params.value;
3541
}),
42+
transformEnableImplicitConversion,
3643
isFileValidator,
37-
IsArray(Object.assign({},validationOptions || {}, {each: false}))
38-
)
44+
IsArray(Object.assign({},validationOptions || {}, { each: false }))
45+
);
3946
}
4047

41-
return isFileValidator
48+
return applyDecorators(
49+
transformEnableImplicitConversion,
50+
isFileValidator
51+
);
4252

43-
44-
}
53+
}

test/array-files-upload.e2e-spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Express - Array files uploads', () => {
1111
app = await createTestModule();
1212
});
1313

14-
it('Valid files upload', () => {
14+
it('Valid files upload - array dto - array files', () => {
1515
return request
1616
.default(app.getHttpServer())
1717
.post('/array-files')
@@ -24,7 +24,7 @@ describe('Express - Array files uploads', () => {
2424
]);
2525
});
2626

27-
it('Valid single file as array', () => {
27+
it('Valid files upload - array dto - single file', () => {
2828
return request
2929
.default(app.getHttpServer())
3030
.post('/array-files')
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { NestFastifyApplication } from '@nestjs/platform-fastify';
3+
import * as request from 'supertest';
4+
import path from 'path';
5+
import { createTestModule } from './helpers/create-test-module';
6+
7+
describe('Express - transform enableImplicitConversion', () => {
8+
let app: INestApplication;
9+
10+
beforeEach(async () => {
11+
app = await createTestModule({}, {
12+
transform: true, transformOptions: {
13+
enableImplicitConversion: true,
14+
},
15+
});
16+
});
17+
18+
it('Valid files upload - array dto - array files', () => {
19+
return request
20+
.default(app.getHttpServer())
21+
.post('/array-files')
22+
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
23+
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
24+
.expect(200)
25+
.expect([
26+
{ filename: 'file.txt', mimetype: 'text/plain' },
27+
{ filename: 'file.txt', mimetype: 'text/plain' },
28+
]);
29+
});
30+
31+
it('Valid files upload - array dto - single file', () => {
32+
return request
33+
.default(app.getHttpServer())
34+
.post('/array-files')
35+
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
36+
.expect(200)
37+
.expect([{ filename: 'file.txt', mimetype: 'text/plain' }]);
38+
});
39+
40+
it('Valid file upload - single dto - single file', () => {
41+
return request
42+
.default(app.getHttpServer())
43+
.post('/single-file')
44+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
45+
.expect(200)
46+
.expect({ filename: 'file.txt', mimetype: 'text/plain' });
47+
});
48+
49+
it('Invalid file upload - single dto - array file', () => {
50+
return request
51+
.default(app.getHttpServer())
52+
.post('/single-file')
53+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
54+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
55+
.expect(400);
56+
});
57+
});
58+
59+
describe('Fastify - transform enableImplicitConversion', () => {
60+
let app: NestFastifyApplication;
61+
62+
beforeEach(async () => {
63+
app = (await createTestModule({
64+
fastify: true,
65+
}, {
66+
transform: true, transformOptions: {
67+
enableImplicitConversion: true,
68+
},
69+
})) as NestFastifyApplication;
70+
});
71+
72+
it('Valid files upload - array dto - array files', () => {
73+
return request
74+
.default(app.getHttpServer())
75+
.post('/array-files')
76+
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
77+
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
78+
.expect(200)
79+
.expect([
80+
{ filename: 'file.txt', mimetype: 'text/plain' },
81+
{ filename: 'file.txt', mimetype: 'text/plain' },
82+
]);
83+
});
84+
85+
it('Valid files upload - array dto - single file', () => {
86+
return request
87+
.default(app.getHttpServer())
88+
.post('/array-files')
89+
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
90+
.expect(200)
91+
.expect([{ filename: 'file.txt', mimetype: 'text/plain' }]);
92+
});
93+
94+
it('Valid file upload - single dto - single file', () => {
95+
return request
96+
.default(app.getHttpServer())
97+
.post('/single-file')
98+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
99+
.expect(200)
100+
.expect({ filename: 'file.txt', mimetype: 'text/plain' });
101+
});
102+
103+
it('Invalid file upload - single dto - array file', () => {
104+
return request
105+
.default(app.getHttpServer())
106+
.post('/single-file')
107+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
108+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
109+
.expect(400);
110+
});
111+
});

test/helpers/create-test-module.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { INestApplication } from '@nestjs/common';
2+
import { INestApplication, ValidationPipe } from '@nestjs/common';
33
import {
44
FastifyAdapter,
55
NestFastifyApplication,
66
} from '@nestjs/platform-fastify';
77
import { TestModule } from '../test-module/test.module';
8+
import { ValidationPipeOptions } from '@nestjs/common/pipes/validation.pipe';
89

910
export async function createTestModule(
10-
config: any = {},
11+
config: any = {}, validationPipeOptions: ValidationPipeOptions = {
12+
transform: true,
13+
}
1114
): Promise<INestApplication | NestFastifyApplication> {
1215
const moduleFixture: TestingModule = await Test.createTestingModule({
1316
imports: [TestModule.config(config)],
@@ -27,6 +30,10 @@ export async function createTestModule(
2730
app = moduleFixture.createNestApplication();
2831
}
2932

33+
app.useGlobalPipes(
34+
new ValidationPipe(validationPipeOptions),
35+
);
36+
3037
await app.init();
3138

3239
if (useFastify) {

test/single-upload.e2e-spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Express - Single file uploads', () => {
1111
app = await createTestModule();
1212
});
1313

14-
it('Valid file upload', () => {
14+
it('Valid file upload - single dto - single file', () => {
1515
return request
1616
.default(app.getHttpServer())
1717
.post('/single-file')
@@ -20,6 +20,15 @@ describe('Express - Single file uploads', () => {
2020
.expect({ filename: 'file.txt', mimetype: 'text/plain' });
2121
});
2222

23+
it('Invalid file upload - single dto - array file', () => {
24+
return request
25+
.default(app.getHttpServer())
26+
.post('/single-file')
27+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
28+
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
29+
.expect(400);
30+
});
31+
2332
it('Mime type validator', () => {
2433
return request
2534
.default(app.getHttpServer())

0 commit comments

Comments
 (0)