Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): define type for file input #11040

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/common/pipes/file/file-type.validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileValidator } from './file-validator.interface';
import { IFile } from './interfaces';

export type FileTypeValidatorOptions = {
fileType: string | RegExp;
Expand All @@ -20,17 +21,14 @@ export class FileTypeValidator extends FileValidator<FileTypeValidatorOptions> {
return `Validation failed (expected type is ${this.validationOptions.fileType})`;
}

isValid(file: any): boolean {
isValid<TFile extends IFile = any>(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)
);
}
}
6 changes: 5 additions & 1 deletion packages/common/pipes/file/file-validator.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IFile } from './interfaces';

/**
* Interface describing FileValidators, which can be added to a {@link ParseFilePipe}.
*
Expand All @@ -10,7 +12,9 @@ export abstract class FileValidator<TValidationOptions = Record<string, any>> {
* 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<boolean>;
abstract isValid<TFile extends IFile = any>(
file?: TFile,
): boolean | Promise<boolean>;

/**
* Builds an error message in case the validation fails.
Expand Down
4 changes: 4 additions & 0 deletions packages/common/pipes/file/interfaces/file.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IFile {
mimetype: string;
size: number;
}
1 change: 1 addition & 0 deletions packages/common/pipes/file/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './file.interface';
5 changes: 3 additions & 2 deletions packages/common/pipes/file/max-file-size.validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileValidator } from './file-validator.interface';
import { IFile } from './interfaces';

export type MaxFileSizeValidatorOptions = {
maxSize: number;
Expand All @@ -16,11 +17,11 @@ export class MaxFileSizeValidator extends FileValidator<MaxFileSizeValidatorOpti
return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`;
}

public isValid(file: any): boolean {
public isValid<TFile extends IFile = any>(file: TFile): boolean {
if (!this.validationOptions) {
return true;
}

return file.size < this.validationOptions.maxSize;
return 'size' in file && file.size < this.validationOptions.maxSize;
}
}
14 changes: 7 additions & 7 deletions packages/common/test/pipes/file/file-type.validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileTypeValidator } from '../../../pipes';
import { expect } from 'chai';
import { FileTypeValidator } from '../../../pipes';

describe('FileTypeValidator', () => {
describe('isValid', () => {
Expand All @@ -10,7 +10,7 @@ describe('FileTypeValidator', () => {

const requestFile = {
mimetype: 'image/jpeg',
};
} as any;

expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
});
Expand All @@ -22,7 +22,7 @@ describe('FileTypeValidator', () => {

const requestFile = {
mimetype: 'image/jpeg',
};
} as any;

expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
});
Expand All @@ -34,7 +34,7 @@ describe('FileTypeValidator', () => {

const requestFile = {
mimetype: 'application/msword',
};
} as any;

expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
});
Expand All @@ -46,7 +46,7 @@ describe('FileTypeValidator', () => {

const requestFile = {
mimetype: 'image/png',
};
} as any;

expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
});
Expand All @@ -58,7 +58,7 @@ describe('FileTypeValidator', () => {

const requestFile = {
mimetype: 'image/png',
};
} as any;

expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
});
Expand All @@ -68,7 +68,7 @@ describe('FileTypeValidator', () => {
fileType: 'image/jpeg',
});

const requestFile = {};
const requestFile = {} as any;

expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('MaxFileSizeValidator', () => {

const requestFile = {
size: 100,
};
} as any;

expect(maxFileSizeValidator.isValid(requestFile)).to.equal(true);
});
Expand All @@ -24,7 +24,7 @@ describe('MaxFileSizeValidator', () => {

const requestFile = {
size: oneKb + 1,
};
} as any;

expect(maxFileSizeValidator.isValid(requestFile)).to.equal(false);
});
Expand All @@ -36,7 +36,7 @@ describe('MaxFileSizeValidator', () => {

const requestFile = {
size: oneKb,
};
} as any;

expect(maxFileSizeValidator.isValid(requestFile)).to.equal(false);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'chai';
import {
FileTypeValidator,
FileTypeValidatorOptions,
FileValidator,
MaxFileSizeValidator,
ParseFilePipeBuilder,
Expand Down