• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nestjs / nest / 34dedede-dd63-4d6b-9fa1-8f953667f765

15 Jul 2025 05:36AM UTC coverage: 88.915% (-0.003%) from 88.918%
34dedede-dd63-4d6b-9fa1-8f953667f765

Pull #15364

circleci

Yasir-Rafique
chore: merge latest package.json files to resolve conflicts
Pull Request #15364: Fix/file type peer dependency

2711 of 3425 branches covered (79.15%)

12 of 14 new or added lines in 1 file covered. (85.71%)

7203 of 8101 relevant lines covered (88.91%)

16.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/packages/common/pipes/file/file-type.validator.ts
1
import { FileValidator } from './file-validator.interface';
1✔
2
import { IFile } from './interfaces';
3
import { loadEsm } from 'load-esm';
1✔
4

5
export type FileTypeValidatorOptions = {
6
  fileType: string | RegExp;
7

8
  /**
9
   * If `true`, the validator will skip the magic numbers validation.
10
   * This can be useful when you can't identify some files as there are no common magic numbers available for some file types.
11
   * @default false
12
   */
13
  skipMagicNumbersValidation?: boolean;
14

15
  /**
16
   * If `true`, and magic number check fails, fallback to mimetype comparison.
17
   * @default false
18
   */
19
  fallbackToMimetype?: boolean;
20
};
21

22
/**
23
 * Defines the built-in FileTypeValidator. It validates incoming files by examining
24
 * their magic numbers using the file-type package, providing more reliable file type validation
25
 * than just checking the mimetype string.
26
 *
27
 * @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
28
 *
29
 * @publicApi
30
 */
31
export class FileTypeValidator extends FileValidator<
1✔
32
  FileTypeValidatorOptions,
33
  IFile
34
> {
35
  buildErrorMessage(file?: IFile): string {
36
    const expected = this.validationOptions.fileType;
4✔
37

38
    if (file?.mimetype) {
4✔
39
      const baseMessage = `Validation failed (current file type is ${file.mimetype}, expected type is ${expected})`;
3✔
40

41
      /**
42
       * If fallbackToMimetype is enabled, this means the validator failed to detect the file type
43
       * via magic number inspection (e.g. due to an unknown or too short buffer),
44
       * and instead used the mimetype string provided by the client as a fallback.
45
       *
46
       * This message clarifies that fallback logic was used, in case users rely on file signatures.
47
       */
48
      if (this.validationOptions.fallbackToMimetype) {
3!
49
        return `${baseMessage} - magic number detection failed, used mimetype fallback`;
×
50
      }
51

52
      return baseMessage;
3✔
53
    }
54

55
    return `Validation failed (expected type is ${expected})`;
1✔
56
  }
57

58
  async isValid(file?: IFile): Promise<boolean> {
59
    const isFileValid = !!file && 'mimetype' in file;
18✔
60
    console.log('isFileValid', isFileValid);
18✔
61

62
    if (!isFileValid || !file.buffer) {
18✔
63
      console.log('Fallback path hit');
4✔
64
      if (
4✔
65
        this.validationOptions?.fallbackToMimetype ||
8✔
66
        this.validationOptions?.skipMagicNumbersValidation
67
      ) {
68
        return !!file?.mimetype?.match(this.validationOptions.fileType);
1✔
69
      }
70
      return false;
3✔
71
    }
72

73
    if (this.validationOptions?.skipMagicNumbersValidation) {
14!
NEW
74
      console.log('Skipping magic number check');
×
NEW
75
      return !!file.mimetype.match(this.validationOptions.fileType);
×
76
    }
77

78
    try {
14✔
79
      const { fileTypeFromBuffer } = await loadEsm<any>('file-type');
14✔
80
      const fileType = await fileTypeFromBuffer(file.buffer);
14✔
81
      console.log('Detected fileType:', fileType);
12✔
82

83
      if (fileType) {
12✔
84
        return !!fileType.mime.match(this.validationOptions.fileType);
6✔
85
      }
86

87
      if (this.validationOptions?.fallbackToMimetype) {
6✔
88
        console.log('Fallback to mimetype:', file.mimetype);
2✔
89
        return !!file.mimetype.match(this.validationOptions.fileType);
2✔
90
      }
91

92
      return false;
4✔
93
    } catch (err) {
94
      console.error('Error in fileTypeFromBuffer:', err);
2✔
95
      return false;
2✔
96
    }
97
  }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc