]>
Commit | Line | Data |
---|---|---|
ac81d1a0 | 1 | import 'multer' |
72c7248b | 2 | import * as validator from 'validator' |
fdbda9e3 | 3 | |
69818c93 | 4 | function exists (value: any) { |
e4c55619 C |
5 | return value !== undefined && value !== null |
6 | } | |
7 | ||
69818c93 | 8 | function isArray (value: any) { |
e4c55619 C |
9 | return Array.isArray(value) |
10 | } | |
11 | ||
2f1548fd C |
12 | function isNotEmptyIntArray (value: any) { |
13 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 | |
cef534ed C |
14 | } |
15 | ||
09209296 C |
16 | function isArrayOf (value: any, validator: (value: any) => boolean) { |
17 | return isArray(value) && value.every(v => validator(v)) | |
18 | } | |
19 | ||
72c7248b C |
20 | function isDateValid (value: string) { |
21 | return exists(value) && validator.isISO8601(value) | |
22 | } | |
23 | ||
24 | function isIdValid (value: string) { | |
25 | return exists(value) && validator.isInt('' + value) | |
26 | } | |
27 | ||
28 | function isUUIDValid (value: string) { | |
29 | return exists(value) && validator.isUUID('' + value, 4) | |
30 | } | |
31 | ||
32 | function isIdOrUUIDValid (value: string) { | |
33 | return isIdValid(value) || isUUIDValid(value) | |
34 | } | |
35 | ||
360329cc | 36 | function isBooleanValid (value: any) { |
47564bbe C |
37 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) |
38 | } | |
39 | ||
360329cc C |
40 | function toIntOrNull (value: string) { |
41 | if (value === 'null') return null | |
42 | ||
43 | return validator.toInt(value) | |
44 | } | |
45 | ||
2efd32f6 | 46 | function toValueOrNull (value: string) { |
360329cc C |
47 | if (value === 'null') return null |
48 | ||
49 | return value | |
50 | } | |
51 | ||
d525fc39 C |
52 | function toArray (value: string) { |
53 | if (value && isArray(value) === false) return [ value ] | |
54 | ||
55 | return value | |
56 | } | |
57 | ||
ac81d1a0 C |
58 | function isFileValid ( |
59 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | |
60 | mimeTypeRegex: string, | |
61 | field: string, | |
c1e791ba | 62 | maxSize: number | null, |
ac81d1a0 C |
63 | optional = false |
64 | ) { | |
65 | // Should have files | |
66 | if (!files) return optional | |
67 | if (isArray(files)) return optional | |
68 | ||
69 | // Should have a file | |
70 | const fileArray = files[ field ] | |
71 | if (!fileArray || fileArray.length === 0) { | |
72 | return optional | |
73 | } | |
74 | ||
75 | // The file should exist | |
76 | const file = fileArray[ 0 ] | |
77 | if (!file || !file.originalname) return false | |
78 | ||
0c237b19 | 79 | // Check size |
c1e791ba | 80 | if ((maxSize !== null) && file.size > maxSize) return false |
0c237b19 | 81 | |
ac81d1a0 C |
82 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) |
83 | } | |
84 | ||
e4c55619 C |
85 | // --------------------------------------------------------------------------- |
86 | ||
65fcc311 C |
87 | export { |
88 | exists, | |
09209296 | 89 | isArrayOf, |
2f1548fd | 90 | isNotEmptyIntArray, |
72c7248b C |
91 | isArray, |
92 | isIdValid, | |
93 | isUUIDValid, | |
94 | isIdOrUUIDValid, | |
47564bbe | 95 | isDateValid, |
2efd32f6 | 96 | toValueOrNull, |
ac81d1a0 | 97 | isBooleanValid, |
360329cc | 98 | toIntOrNull, |
d525fc39 | 99 | toArray, |
ac81d1a0 | 100 | isFileValid |
65fcc311 | 101 | } |