Skip to content
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
5 changes: 4 additions & 1 deletion packages/common/pipes/file/file-type.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export class FileTypeValidator extends FileValidator<
FileTypeValidatorOptions,
IFile
> {
buildErrorMessage(): string {
buildErrorMessage(file?: IFile): string {
if (file?.mimetype) {
return `Validation failed (current file type is ${file.mimetype}, expected type is ${this.validationOptions.fileType})`;
}
return `Validation failed (expected type is ${this.validationOptions.fileType})`;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/common/pipes/file/max-file-size.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MaxFileSizeValidator extends FileValidator<
MaxFileSizeValidatorOptions,
IFile
> {
buildErrorMessage(): string {
buildErrorMessage(file?: IFile): string {
if ('message' in this.validationOptions) {
if (typeof this.validationOptions.message === 'function') {
return this.validationOptions.message(this.validationOptions.maxSize);
Expand All @@ -26,6 +26,9 @@ export class MaxFileSizeValidator extends FileValidator<
return this.validationOptions.message!;
}

if (file?.size) {
return `Validation failed (current file size is ${file.size}, expected size is less than ${this.validationOptions.maxSize})`;
}
return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/common/test/pipes/file/file-type.validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,19 @@ describe('FileTypeValidator', () => {
`Validation failed (expected type is ${fileType})`,
);
});

it('should include the file type in the error message when a file is provided', () => {
const currentFileType = 'image/png';
const fileType = 'image/jpeg';
const fileTypeValidator = new FileTypeValidator({
fileType,
});

const file = { mimetype: currentFileType } as any;

expect(fileTypeValidator.buildErrorMessage(file)).to.equal(
`Validation failed (current file type is ${currentFileType}, expected type is ${fileType})`,
);
});
});
});
13 changes: 13 additions & 0 deletions packages/common/test/pipes/file/max-file-size.validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,18 @@ describe('MaxFileSizeValidator', () => {
`Validation failed (expected size is less than ${oneKb})`,
);
});

it('should include the file size in the error message when a file is provided', () => {
const currentFileSize = oneKb + 1;
const maxFileSizeValidator = new MaxFileSizeValidator({
maxSize: oneKb,
});

const file = { size: currentFileSize } as any;

expect(maxFileSizeValidator.buildErrorMessage(file)).to.equal(
`Validation failed (current file size is ${currentFileSize}, expected size is less than ${oneKb})`,
);
});
});
});