diff --git a/packages/common/pipes/file/file-type.validator.ts b/packages/common/pipes/file/file-type.validator.ts index 240d3efe86a..ffc3e54c516 100644 --- a/packages/common/pipes/file/file-type.validator.ts +++ b/packages/common/pipes/file/file-type.validator.ts @@ -1,4 +1,5 @@ import { FileValidator } from './file-validator.interface'; +import { IFile } from './interfaces'; export type FileTypeValidatorOptions = { fileType: string | RegExp; @@ -20,17 +21,14 @@ export class FileTypeValidator extends FileValidator { return `Validation failed (expected type is ${this.validationOptions.fileType})`; } - isValid(file: any): boolean { + isValid(file: TFile): boolean { if (!this.validationOptions) { return true; } - if (!file.mimetype) { - return false; - } - - return Boolean( - (file.mimetype as string).match(this.validationOptions.fileType), + return ( + 'mimetype' in file && + !!file.mimetype.match(this.validationOptions.fileType) ); } } diff --git a/packages/common/pipes/file/file-validator.interface.ts b/packages/common/pipes/file/file-validator.interface.ts index 4da506a8c9d..1d31b70d0d6 100644 --- a/packages/common/pipes/file/file-validator.interface.ts +++ b/packages/common/pipes/file/file-validator.interface.ts @@ -1,3 +1,5 @@ +import { IFile } from './interfaces'; + /** * Interface describing FileValidators, which can be added to a {@link ParseFilePipe}. * @@ -10,7 +12,9 @@ export abstract class FileValidator> { * Indicates if this file should be considered valid, according to the options passed in the constructor. * @param file the file from the request object */ - abstract isValid(file?: any): boolean | Promise; + abstract isValid( + file?: TFile, + ): boolean | Promise; /** * Builds an error message in case the validation fails. diff --git a/packages/common/pipes/file/interfaces/file.interface.ts b/packages/common/pipes/file/interfaces/file.interface.ts new file mode 100644 index 00000000000..37218734fa7 --- /dev/null +++ b/packages/common/pipes/file/interfaces/file.interface.ts @@ -0,0 +1,4 @@ +export interface IFile { + mimetype: string; + size: number; +} diff --git a/packages/common/pipes/file/interfaces/index.ts b/packages/common/pipes/file/interfaces/index.ts new file mode 100644 index 00000000000..09d8f3ca8f1 --- /dev/null +++ b/packages/common/pipes/file/interfaces/index.ts @@ -0,0 +1 @@ +export * from './file.interface'; diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index 847ee19f07b..9ea3dc7d760 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -1,4 +1,5 @@ import { FileValidator } from './file-validator.interface'; +import { IFile } from './interfaces'; export type MaxFileSizeValidatorOptions = { maxSize: number; @@ -16,11 +17,11 @@ export class MaxFileSizeValidator extends FileValidator(file: TFile): boolean { if (!this.validationOptions) { return true; } - return file.size < this.validationOptions.maxSize; + return 'size' in file && file.size < this.validationOptions.maxSize; } } diff --git a/packages/common/test/pipes/file/file-type.validator.spec.ts b/packages/common/test/pipes/file/file-type.validator.spec.ts index 8b58d114033..ec562aed01e 100644 --- a/packages/common/test/pipes/file/file-type.validator.spec.ts +++ b/packages/common/test/pipes/file/file-type.validator.spec.ts @@ -1,5 +1,5 @@ -import { FileTypeValidator } from '../../../pipes'; import { expect } from 'chai'; +import { FileTypeValidator } from '../../../pipes'; describe('FileTypeValidator', () => { describe('isValid', () => { @@ -10,7 +10,7 @@ describe('FileTypeValidator', () => { const requestFile = { mimetype: 'image/jpeg', - }; + } as any; expect(fileTypeValidator.isValid(requestFile)).to.equal(true); }); @@ -22,7 +22,7 @@ describe('FileTypeValidator', () => { const requestFile = { mimetype: 'image/jpeg', - }; + } as any; expect(fileTypeValidator.isValid(requestFile)).to.equal(true); }); @@ -34,7 +34,7 @@ describe('FileTypeValidator', () => { const requestFile = { mimetype: 'application/msword', - }; + } as any; expect(fileTypeValidator.isValid(requestFile)).to.equal(true); }); @@ -46,7 +46,7 @@ describe('FileTypeValidator', () => { const requestFile = { mimetype: 'image/png', - }; + } as any; expect(fileTypeValidator.isValid(requestFile)).to.equal(false); }); @@ -58,7 +58,7 @@ describe('FileTypeValidator', () => { const requestFile = { mimetype: 'image/png', - }; + } as any; expect(fileTypeValidator.isValid(requestFile)).to.equal(false); }); @@ -68,7 +68,7 @@ describe('FileTypeValidator', () => { fileType: 'image/jpeg', }); - const requestFile = {}; + const requestFile = {} as any; expect(fileTypeValidator.isValid(requestFile)).to.equal(false); }); diff --git a/packages/common/test/pipes/file/max-file-size.validator.spec.ts b/packages/common/test/pipes/file/max-file-size.validator.spec.ts index 9ba8e4283bc..61d49e6e86f 100644 --- a/packages/common/test/pipes/file/max-file-size.validator.spec.ts +++ b/packages/common/test/pipes/file/max-file-size.validator.spec.ts @@ -12,7 +12,7 @@ describe('MaxFileSizeValidator', () => { const requestFile = { size: 100, - }; + } as any; expect(maxFileSizeValidator.isValid(requestFile)).to.equal(true); }); @@ -24,7 +24,7 @@ describe('MaxFileSizeValidator', () => { const requestFile = { size: oneKb + 1, - }; + } as any; expect(maxFileSizeValidator.isValid(requestFile)).to.equal(false); }); @@ -36,7 +36,7 @@ describe('MaxFileSizeValidator', () => { const requestFile = { size: oneKb, - }; + } as any; expect(maxFileSizeValidator.isValid(requestFile)).to.equal(false); }); diff --git a/packages/common/test/pipes/file/parse-file-pipe.builder.spec.ts b/packages/common/test/pipes/file/parse-file-pipe.builder.spec.ts index 43f1b29a1c5..784cea3de55 100644 --- a/packages/common/test/pipes/file/parse-file-pipe.builder.spec.ts +++ b/packages/common/test/pipes/file/parse-file-pipe.builder.spec.ts @@ -1,7 +1,6 @@ import { expect } from 'chai'; import { FileTypeValidator, - FileTypeValidatorOptions, FileValidator, MaxFileSizeValidator, ParseFilePipeBuilder,