diff --git a/packages/common/pipes/file/file-type.validator.ts b/packages/common/pipes/file/file-type.validator.ts index e18ae156075..a47f134e92c 100644 --- a/packages/common/pipes/file/file-type.validator.ts +++ b/packages/common/pipes/file/file-type.validator.ts @@ -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})`; } diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index 024bc532411..c8307de1dc6 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -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); @@ -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})`; } 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 0daf2b5d27d..8ac3e971ae9 100644 --- a/packages/common/test/pipes/file/file-type.validator.spec.ts +++ b/packages/common/test/pipes/file/file-type.validator.spec.ts @@ -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})`, + ); + }); }); }); 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 8bc6c2ea1bf..a98f377aece 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 @@ -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})`, + ); + }); }); });