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