]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/misc.ts
Fix express validator
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
1 import 'multer'
2 import * as validator from 'validator'
3 import { sep } from 'path'
4 import toBoolean = require('validator/lib/toBoolean')
5
6 function exists (value: any) {
7 return value !== undefined && value !== null
8 }
9
10 function isSafePath (p: string) {
11 return exists(p) &&
12 (p + '').split(sep).every(part => {
13 return [ '..' ].includes(part) === false
14 })
15 }
16
17 function isArray (value: any) {
18 return Array.isArray(value)
19 }
20
21 function isNotEmptyIntArray (value: any) {
22 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
23 }
24
25 function isArrayOf (value: any, validator: (value: any) => boolean) {
26 return isArray(value) && value.every(v => validator(v))
27 }
28
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
45 function isBooleanValid (value: any) {
46 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
47 }
48
49 function toIntOrNull (value: string) {
50 const v = toValueOrNull(value)
51
52 if (v === null || v === undefined) return v
53 if (typeof v === 'number') return v
54
55 return validator.toInt(v)
56 }
57
58 function toBooleanOrNull (value: any) {
59 const v = toValueOrNull(value)
60
61 if (v === null || v === undefined) return v
62 if (typeof v === 'boolean') return v
63
64 return toBoolean(v)
65 }
66
67 function toValueOrNull (value: string) {
68 if (value === 'null') return null
69
70 return value
71 }
72
73 function toArray (value: any) {
74 if (value && isArray(value) === false) return [ value ]
75
76 return value
77 }
78
79 function toIntArray (value: any) {
80 if (!value) return []
81 if (isArray(value) === false) return [ validator.toInt(value) ]
82
83 return value.map(v => validator.toInt(v))
84 }
85
86 function isFileValid (
87 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
88 mimeTypeRegex: string,
89 field: string,
90 maxSize: number | null,
91 optional = false
92 ) {
93 // Should have files
94 if (!files) return optional
95 if (isArray(files)) return optional
96
97 // Should have a file
98 const fileArray = files[ field ]
99 if (!fileArray || fileArray.length === 0) {
100 return optional
101 }
102
103 // The file should exist
104 const file = fileArray[ 0 ]
105 if (!file || !file.originalname) return false
106
107 // Check size
108 if ((maxSize !== null) && file.size > maxSize) return false
109
110 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
111 }
112
113 // ---------------------------------------------------------------------------
114
115 export {
116 exists,
117 isArrayOf,
118 isNotEmptyIntArray,
119 isArray,
120 isIdValid,
121 isSafePath,
122 isUUIDValid,
123 isIdOrUUIDValid,
124 isDateValid,
125 toValueOrNull,
126 toBooleanOrNull,
127 isBooleanValid,
128 toIntOrNull,
129 toArray,
130 toIntArray,
131 isFileValid
132 }