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