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