diff options
Diffstat (limited to 'server/helpers/custom-validators/misc.ts')
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 3903884ea..8a270b777 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | import 'multer' | ||
1 | import * as validator from 'validator' | 2 | import * as validator from 'validator' |
2 | 3 | ||
3 | function exists (value: any) { | 4 | function exists (value: any) { |
@@ -28,6 +29,29 @@ function isBooleanValid (value: string) { | |||
28 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) | 29 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) |
29 | } | 30 | } |
30 | 31 | ||
32 | function isFileValid ( | ||
33 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | ||
34 | mimeTypeRegex: string, | ||
35 | field: string, | ||
36 | optional = false | ||
37 | ) { | ||
38 | // Should have files | ||
39 | if (!files) return optional | ||
40 | if (isArray(files)) return optional | ||
41 | |||
42 | // Should have a file | ||
43 | const fileArray = files[ field ] | ||
44 | if (!fileArray || fileArray.length === 0) { | ||
45 | return optional | ||
46 | } | ||
47 | |||
48 | // The file should exist | ||
49 | const file = fileArray[ 0 ] | ||
50 | if (!file || !file.originalname) return false | ||
51 | |||
52 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) | ||
53 | } | ||
54 | |||
31 | // --------------------------------------------------------------------------- | 55 | // --------------------------------------------------------------------------- |
32 | 56 | ||
33 | export { | 57 | export { |
@@ -37,5 +61,6 @@ export { | |||
37 | isUUIDValid, | 61 | isUUIDValid, |
38 | isIdOrUUIDValid, | 62 | isIdOrUUIDValid, |
39 | isDateValid, | 63 | isDateValid, |
40 | isBooleanValid | 64 | isBooleanValid, |
65 | isFileValid | ||
41 | } | 66 | } |