]>
Commit | Line | Data |
---|---|---|
ac81d1a0 | 1 | import 'multer' |
f6d6e7f8 | 2 | import { UploadFilesForCheck } from 'express' |
345da516 | 3 | import { sep } from 'path' |
f6d6e7f8 | 4 | import validator from 'validator' |
fdbda9e3 | 5 | |
69818c93 | 6 | function exists (value: any) { |
e4c55619 C |
7 | return value !== undefined && value !== null |
8 | } | |
9 | ||
345da516 C |
10 | function isSafePath (p: string) { |
11 | return exists(p) && | |
12 | (p + '').split(sep).every(part => { | |
f023a19c | 13 | return [ '..' ].includes(part) === false |
345da516 C |
14 | }) |
15 | } | |
16 | ||
c158a5fa | 17 | function isArray (value: any): value is any[] { |
e4c55619 C |
18 | return Array.isArray(value) |
19 | } | |
20 | ||
2f1548fd C |
21 | function isNotEmptyIntArray (value: any) { |
22 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 | |
cef534ed C |
23 | } |
24 | ||
09209296 C |
25 | function isArrayOf (value: any, validator: (value: any) => boolean) { |
26 | return isArray(value) && value.every(v => validator(v)) | |
27 | } | |
28 | ||
72c7248b C |
29 | function isDateValid (value: string) { |
30 | return exists(value) && validator.isISO8601(value) | |
31 | } | |
32 | ||
33 | function isIdValid (value: string) { | |
34 | return exists(value) && validator.isInt('' + value) | |
35 | } | |
36 | ||
37 | function isUUIDValid (value: string) { | |
38 | return exists(value) && validator.isUUID('' + value, 4) | |
39 | } | |
40 | ||
41 | function isIdOrUUIDValid (value: string) { | |
42 | return isIdValid(value) || isUUIDValid(value) | |
43 | } | |
44 | ||
360329cc | 45 | function isBooleanValid (value: any) { |
47564bbe C |
46 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) |
47 | } | |
48 | ||
fb719404 C |
49 | function isIntOrNull (value: any) { |
50 | return value === null || validator.isInt('' + value) | |
51 | } | |
52 | ||
360329cc | 53 | function toIntOrNull (value: string) { |
c8861d5d C |
54 | const v = toValueOrNull(value) |
55 | ||
56 | if (v === null || v === undefined) return v | |
57 | if (typeof v === 'number') return v | |
58 | ||
2b65c4e5 | 59 | return validator.toInt('' + v) |
c8861d5d C |
60 | } |
61 | ||
62 | function toBooleanOrNull (value: any) { | |
63 | const v = toValueOrNull(value) | |
64 | ||
65 | if (v === null || v === undefined) return v | |
66 | if (typeof v === 'boolean') return v | |
360329cc | 67 | |
2b65c4e5 | 68 | return validator.toBoolean('' + v) |
360329cc C |
69 | } |
70 | ||
2efd32f6 | 71 | function toValueOrNull (value: string) { |
360329cc C |
72 | if (value === 'null') return null |
73 | ||
74 | return value | |
75 | } | |
76 | ||
f0a39880 | 77 | function toArray (value: any) { |
d525fc39 C |
78 | if (value && isArray(value) === false) return [ value ] |
79 | ||
80 | return value | |
81 | } | |
82 | ||
f0a39880 C |
83 | function toIntArray (value: any) { |
84 | if (!value) return [] | |
85 | if (isArray(value) === false) return [ validator.toInt(value) ] | |
86 | ||
87 | return value.map(v => validator.toInt(v)) | |
88 | } | |
89 | ||
f2eb23cd RK |
90 | function isFileFieldValid ( |
91 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | |
92 | field: string, | |
93 | optional = false | |
94 | ) { | |
95 | // Should have files | |
96 | if (!files) return optional | |
97 | if (isArray(files)) return optional | |
98 | ||
99 | // Should have a file | |
100 | const fileArray = files[field] | |
101 | if (!fileArray || fileArray.length === 0) { | |
102 | return optional | |
103 | } | |
104 | ||
105 | // The file should exist | |
106 | const file = fileArray[0] | |
107 | if (!file || !file.originalname) return false | |
108 | return file | |
109 | } | |
110 | ||
111 | function isFileMimeTypeValid ( | |
f6d6e7f8 | 112 | files: UploadFilesForCheck, |
f2eb23cd RK |
113 | mimeTypeRegex: string, |
114 | field: string, | |
115 | optional = false | |
116 | ) { | |
117 | // Should have files | |
118 | if (!files) return optional | |
119 | if (isArray(files)) return optional | |
120 | ||
121 | // Should have a file | |
122 | const fileArray = files[field] | |
123 | if (!fileArray || fileArray.length === 0) { | |
124 | return optional | |
125 | } | |
126 | ||
127 | // The file should exist | |
128 | const file = fileArray[0] | |
129 | if (!file || !file.originalname) return false | |
130 | ||
131 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) | |
132 | } | |
133 | ||
ac81d1a0 C |
134 | function isFileValid ( |
135 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | |
136 | mimeTypeRegex: string, | |
137 | field: string, | |
c1e791ba | 138 | maxSize: number | null, |
ac81d1a0 C |
139 | optional = false |
140 | ) { | |
141 | // Should have files | |
142 | if (!files) return optional | |
143 | if (isArray(files)) return optional | |
144 | ||
145 | // Should have a file | |
a1587156 | 146 | const fileArray = files[field] |
ac81d1a0 C |
147 | if (!fileArray || fileArray.length === 0) { |
148 | return optional | |
149 | } | |
150 | ||
151 | // The file should exist | |
a1587156 | 152 | const file = fileArray[0] |
ac81d1a0 C |
153 | if (!file || !file.originalname) return false |
154 | ||
0c237b19 | 155 | // Check size |
c1e791ba | 156 | if ((maxSize !== null) && file.size > maxSize) return false |
0c237b19 | 157 | |
ac81d1a0 C |
158 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) |
159 | } | |
160 | ||
e4c55619 C |
161 | // --------------------------------------------------------------------------- |
162 | ||
65fcc311 C |
163 | export { |
164 | exists, | |
09209296 | 165 | isArrayOf, |
2f1548fd | 166 | isNotEmptyIntArray, |
72c7248b | 167 | isArray, |
fb719404 | 168 | isIntOrNull, |
72c7248b | 169 | isIdValid, |
345da516 | 170 | isSafePath, |
72c7248b C |
171 | isUUIDValid, |
172 | isIdOrUUIDValid, | |
47564bbe | 173 | isDateValid, |
2efd32f6 | 174 | toValueOrNull, |
c8861d5d | 175 | toBooleanOrNull, |
ac81d1a0 | 176 | isBooleanValid, |
360329cc | 177 | toIntOrNull, |
d525fc39 | 178 | toArray, |
f0a39880 | 179 | toIntArray, |
f2eb23cd RK |
180 | isFileFieldValid, |
181 | isFileMimeTypeValid, | |
ac81d1a0 | 182 | isFileValid |
65fcc311 | 183 | } |