Skip to content

Commit 047832c

Browse files
committed
feat(common): add error messages for file validators
1 parent 67e30e2 commit 047832c

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

packages/common/pipes/file/file-type.validator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export class FileTypeValidator extends FileValidator<
2020
FileTypeValidatorOptions,
2121
IFile
2222
> {
23-
buildErrorMessage(): string {
23+
buildErrorMessage(file?: IFile): string {
24+
if (file?.mimetype) {
25+
return `Validation failed (current file type is ${file.mimetype}, expected type is ${this.validationOptions.fileType})`;
26+
}
2427
return `Validation failed (expected type is ${this.validationOptions.fileType})`;
2528
}
2629

packages/common/pipes/file/max-file-size.validator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class MaxFileSizeValidator extends FileValidator<
1717
MaxFileSizeValidatorOptions,
1818
IFile
1919
> {
20-
buildErrorMessage(): string {
20+
buildErrorMessage(file?: IFile): string {
2121
if ('message' in this.validationOptions) {
2222
if (typeof this.validationOptions.message === 'function') {
2323
return this.validationOptions.message(this.validationOptions.maxSize);
@@ -26,6 +26,9 @@ export class MaxFileSizeValidator extends FileValidator<
2626
return this.validationOptions.message;
2727
}
2828

29+
if (file?.size) {
30+
return `Validation failed (current file size is ${file.size}, expected size is less than ${this.validationOptions.maxSize})`;
31+
}
2932
return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`;
3033
}
3134

packages/common/test/pipes/file/file-type.validator.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,19 @@ describe('FileTypeValidator', () => {
9393
`Validation failed (expected type is ${fileType})`,
9494
);
9595
});
96+
97+
it('should include the file type in the error message when a file is provided', () => {
98+
const currentFileType = 'image/png';
99+
const fileType = 'image/jpeg';
100+
const fileTypeValidator = new FileTypeValidator({
101+
fileType,
102+
});
103+
104+
const file = { mimetype: currentFileType } as any;
105+
106+
expect(fileTypeValidator.buildErrorMessage(file)).to.equal(
107+
`Validation failed (current file type is ${currentFileType}, expected type is ${fileType})`,
108+
);
109+
});
96110
});
97111
});

packages/common/test/pipes/file/max-file-size.validator.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,18 @@ describe('MaxFileSizeValidator', () => {
6060
`Validation failed (expected size is less than ${oneKb})`,
6161
);
6262
});
63+
64+
it('should include the file size in the error message when a file is provided', () => {
65+
const currentFileSize = oneKb + 1;
66+
const maxFileSizeValidator = new MaxFileSizeValidator({
67+
maxSize: oneKb,
68+
});
69+
70+
const file = { size: currentFileSize } as any;
71+
72+
expect(maxFileSizeValidator.buildErrorMessage(file)).to.equal(
73+
`Validation failed (current file size is ${currentFileSize}, expected size is less than ${oneKb})`,
74+
);
75+
});
6376
});
6477
});

0 commit comments

Comments
 (0)