-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mimeTypes validation for uploads
- Loading branch information
Showing
5 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { mimeTypeValidator } from './mimeTypeValidator'; | ||
|
||
describe('mimeTypeValidator', () => { | ||
it('should validate single mimeType', () => { | ||
const mimeTypes = ['image/png']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('image/png')).toBe(true); | ||
}); | ||
|
||
it('should validate multiple mimeTypes', () => { | ||
const mimeTypes = ['image/png', 'application/pdf']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('image/png')).toBe(true); | ||
expect(validate('application/pdf')).toBe(true); | ||
}); | ||
|
||
it('should validate using wildcard', () => { | ||
const mimeTypes = ['image/*']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('image/png')).toBe(true); | ||
expect(validate('image/gif')).toBe(true); | ||
}); | ||
|
||
it('should validate multiple wildcards', () => { | ||
const mimeTypes = ['image/*', 'audio/*']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('image/png')).toBe(true); | ||
expect(validate('audio/mpeg')).toBe(true); | ||
}); | ||
|
||
it('should not validate when unmatched', () => { | ||
const mimeTypes = ['image/png']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('audio/mpeg')).toBe('Invalid file type: \'audio/mpeg\''); | ||
}); | ||
|
||
it('should not validate when unmatched - multiple mimeTypes', () => { | ||
const mimeTypes = ['image/png', 'application/pdf']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('audio/mpeg')).toBe('Invalid file type: \'audio/mpeg\''); | ||
}); | ||
|
||
it('should not validate using wildcard - unmatched', () => { | ||
const mimeTypes = ['image/*']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('audio/mpeg')).toBe('Invalid file type: \'audio/mpeg\''); | ||
}); | ||
|
||
it('should not validate multiple wildcards - unmatched', () => { | ||
const mimeTypes = ['image/*', 'audio/*']; | ||
const validate = mimeTypeValidator(mimeTypes); | ||
expect(validate('video/mp4')).toBe('Invalid file type: \'video/mp4\''); | ||
expect(validate('application/pdf')).toBe('Invalid file type: \'application/pdf\''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Validate } from '../config/types'; | ||
|
||
export const mimeTypeValidator = (mimeTypes: string[]): Validate => (val: string) => { | ||
const cleanedMimeTypes = mimeTypes.map((v) => v.replace('*', '')); | ||
|
||
return !cleanedMimeTypes.some((v) => val.startsWith(v)) | ||
? `Invalid file type: '${val}'` | ||
: true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters