Skip to content

Commit

Permalink
- Resolved [Issue 60](#60)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
dmitriy-nz committed Jun 23, 2024
1 parent 7dec6bf commit 58e733f
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# compiled output
.npmrc
/dist
/node_modules

Expand Down Expand Up @@ -31,4 +32,4 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.npmrc
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### 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.7",
"version": "1.9.9",
"description": "NestJS middleware for handling multipart/form-data, which is primarily used for uploading files",
"main": "dist/index",
"types": "dist/index",
Expand Down
19 changes: 14 additions & 5 deletions src/decorators/validation/is-file.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export function IsFile(validationOptions?: ValidationOptions): PropertyDecorator
},
}, validationOptions);

const transformEnableImplicitConversion = Transform(params => {
return params.obj[params.key]
})



if(validationOptions?.each){
return applyDecorators(
Transform((params: TransformFnParams) => {
Expand All @@ -33,12 +39,15 @@ export function IsFile(validationOptions?: ValidationOptions): PropertyDecorator
}
return params.value;
}),
transformEnableImplicitConversion,
isFileValidator,
IsArray(Object.assign({},validationOptions || {}, {each: false}))
)
IsArray(Object.assign({},validationOptions || {}, { each: false }))
);
}

return isFileValidator
return applyDecorators(
transformEnableImplicitConversion,
isFileValidator
);


}
}
4 changes: 2 additions & 2 deletions test/array-files-upload.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Express - Array files uploads', () => {
app = await createTestModule();
});

it('Valid files upload', () => {
it('Valid files upload - array dto - array files', () => {
return request
.default(app.getHttpServer())
.post('/array-files')
Expand All @@ -24,7 +24,7 @@ describe('Express - Array files uploads', () => {
]);
});

it('Valid single file as array', () => {
it('Valid files upload - array dto - single file', () => {
return request
.default(app.getHttpServer())
.post('/array-files')
Expand Down
111 changes: 111 additions & 0 deletions test/enable-implicit-conversion.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { INestApplication } from '@nestjs/common';
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import * as request from 'supertest';
import path from 'path';
import { createTestModule } from './helpers/create-test-module';

describe('Express - transform enableImplicitConversion', () => {
let app: INestApplication;

beforeEach(async () => {
app = await createTestModule({}, {
transform: true, transformOptions: {
enableImplicitConversion: true,
},
});
});

it('Valid files upload - array dto - array files', () => {
return request
.default(app.getHttpServer())
.post('/array-files')
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(200)
.expect([
{ filename: 'file.txt', mimetype: 'text/plain' },
{ filename: 'file.txt', mimetype: 'text/plain' },
]);
});

it('Valid files upload - array dto - single file', () => {
return request
.default(app.getHttpServer())
.post('/array-files')
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(200)
.expect([{ filename: 'file.txt', mimetype: 'text/plain' }]);
});

it('Valid file upload - single dto - single file', () => {
return request
.default(app.getHttpServer())
.post('/single-file')
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(200)
.expect({ filename: 'file.txt', mimetype: 'text/plain' });
});

it('Invalid file upload - single dto - array file', () => {
return request
.default(app.getHttpServer())
.post('/single-file')
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(400);
});
});

describe('Fastify - transform enableImplicitConversion', () => {
let app: NestFastifyApplication;

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

it('Valid files upload - array dto - array files', () => {
return request
.default(app.getHttpServer())
.post('/array-files')
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(200)
.expect([
{ filename: 'file.txt', mimetype: 'text/plain' },
{ filename: 'file.txt', mimetype: 'text/plain' },
]);
});

it('Valid files upload - array dto - single file', () => {
return request
.default(app.getHttpServer())
.post('/array-files')
.attach('files', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(200)
.expect([{ filename: 'file.txt', mimetype: 'text/plain' }]);
});

it('Valid file upload - single dto - single file', () => {
return request
.default(app.getHttpServer())
.post('/single-file')
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(200)
.expect({ filename: 'file.txt', mimetype: 'text/plain' });
});

it('Invalid file upload - single dto - array file', () => {
return request
.default(app.getHttpServer())
.post('/single-file')
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(400);
});
});
11 changes: 9 additions & 2 deletions test/helpers/create-test-module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { TestModule } from '../test-module/test.module';
import { ValidationPipeOptions } from '@nestjs/common/pipes/validation.pipe';

export async function createTestModule(
config: any = {},
config: any = {}, validationPipeOptions: ValidationPipeOptions = {
transform: true,
}
): Promise<INestApplication | NestFastifyApplication> {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [TestModule.config(config)],
Expand All @@ -27,6 +30,10 @@ export async function createTestModule(
app = moduleFixture.createNestApplication();
}

app.useGlobalPipes(
new ValidationPipe(validationPipeOptions),
);

await app.init();

if (useFastify) {
Expand Down
11 changes: 10 additions & 1 deletion test/single-upload.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Express - Single file uploads', () => {
app = await createTestModule();
});

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

it('Invalid file upload - single dto - array file', () => {
return request
.default(app.getHttpServer())
.post('/single-file')
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.attach('file', path.resolve(__dirname, 'test-files', 'file.txt'))
.expect(400);
});

it('Mime type validator', () => {
return request
.default(app.getHttpServer())
Expand Down

0 comments on commit 58e733f

Please sign in to comment.