Move to eslint
[oweals/peertube.git] / server / helpers / custom-validators / misc.ts
1 import 'multer'
2 import validator from 'validator'
3 import { sep } from 'path'
4
5 function exists (value: any) {
6   return value !== undefined && value !== null
7 }
8
9 function isSafePath (p: string) {
10   return exists(p) &&
11     (p + '').split(sep).every(part => {
12       return [ '..' ].includes(part) === false
13     })
14 }
15
16 function isArray (value: any) {
17   return Array.isArray(value)
18 }
19
20 function isNotEmptyIntArray (value: any) {
21   return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
22 }
23
24 function isArrayOf (value: any, validator: (value: any) => boolean) {
25   return isArray(value) && value.every(v => validator(v))
26 }
27
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
44 function isBooleanValid (value: any) {
45   return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
46 }
47
48 function toIntOrNull (value: string) {
49   const v = toValueOrNull(value)
50
51   if (v === null || v === undefined) return v
52   if (typeof v === 'number') return v
53
54   return validator.toInt('' + v)
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
62
63   return validator.toBoolean('' + v)
64 }
65
66 function toValueOrNull (value: string) {
67   if (value === 'null') return null
68
69   return value
70 }
71
72 function toArray (value: any) {
73   if (value && isArray(value) === false) return [ value ]
74
75   return value
76 }
77
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
85 function isFileValid (
86   files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
87   mimeTypeRegex: string,
88   field: string,
89   maxSize: number | null,
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
106   // Check size
107   if ((maxSize !== null) && file.size > maxSize) return false
108
109   return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
110 }
111
112 // ---------------------------------------------------------------------------
113
114 export {
115   exists,
116   isArrayOf,
117   isNotEmptyIntArray,
118   isArray,
119   isIdValid,
120   isSafePath,
121   isUUIDValid,
122   isIdOrUUIDValid,
123   isDateValid,
124   toValueOrNull,
125   toBooleanOrNull,
126   isBooleanValid,
127   toIntOrNull,
128   toArray,
129   toIntArray,
130   isFileValid
131 }