]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/misc.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
CommitLineData
ac81d1a0 1import 'multer'
7cde3b9c 2import validator from 'validator'
345da516 3import { sep } from 'path'
fdbda9e3 4
69818c93 5function exists (value: any) {
e4c55619
C
6 return value !== undefined && value !== null
7}
8
345da516
C
9function 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 16function isArray (value: any) {
e4c55619
C
17 return Array.isArray(value)
18}
19
2f1548fd
C
20function isNotEmptyIntArray (value: any) {
21 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
cef534ed
C
22}
23
09209296
C
24function isArrayOf (value: any, validator: (value: any) => boolean) {
25 return isArray(value) && value.every(v => validator(v))
26}
27
72c7248b
C
28function isDateValid (value: string) {
29 return exists(value) && validator.isISO8601(value)
30}
31
32function isIdValid (value: string) {
33 return exists(value) && validator.isInt('' + value)
34}
35
36function isUUIDValid (value: string) {
37 return exists(value) && validator.isUUID('' + value, 4)
38}
39
40function isIdOrUUIDValid (value: string) {
41 return isIdValid(value) || isUUIDValid(value)
42}
43
360329cc 44function isBooleanValid (value: any) {
47564bbe
C
45 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
46}
47
fb719404
C
48function isIntOrNull (value: any) {
49 return value === null || validator.isInt('' + value)
50}
51
360329cc 52function toIntOrNull (value: string) {
c8861d5d
C
53 const v = toValueOrNull(value)
54
55 if (v === null || v === undefined) return v
56 if (typeof v === 'number') return v
57
2b65c4e5 58 return validator.toInt('' + v)
c8861d5d
C
59}
60
61function toBooleanOrNull (value: any) {
62 const v = toValueOrNull(value)
63
64 if (v === null || v === undefined) return v
65 if (typeof v === 'boolean') return v
360329cc 66
2b65c4e5 67 return validator.toBoolean('' + v)
360329cc
C
68}
69
2efd32f6 70function toValueOrNull (value: string) {
360329cc
C
71 if (value === 'null') return null
72
73 return value
74}
75
f0a39880 76function toArray (value: any) {
d525fc39
C
77 if (value && isArray(value) === false) return [ value ]
78
79 return value
80}
81
f0a39880
C
82function toIntArray (value: any) {
83 if (!value) return []
84 if (isArray(value) === false) return [ validator.toInt(value) ]
85
86 return value.map(v => validator.toInt(v))
87}
88
f2eb23cd
RK
89function isFileFieldValid (
90 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
91 field: string,
92 optional = false
93) {
94 // Should have files
95 if (!files) return optional
96 if (isArray(files)) return optional
97
98 // Should have a file
99 const fileArray = files[field]
100 if (!fileArray || fileArray.length === 0) {
101 return optional
102 }
103
104 // The file should exist
105 const file = fileArray[0]
106 if (!file || !file.originalname) return false
107 return file
108}
109
110function isFileMimeTypeValid (
111 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
112 mimeTypeRegex: string,
113 field: string,
114 optional = false
115) {
116 // Should have files
117 if (!files) return optional
118 if (isArray(files)) return optional
119
120 // Should have a file
121 const fileArray = files[field]
122 if (!fileArray || fileArray.length === 0) {
123 return optional
124 }
125
126 // The file should exist
127 const file = fileArray[0]
128 if (!file || !file.originalname) return false
129
130 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
131}
132
ac81d1a0
C
133function isFileValid (
134 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
135 mimeTypeRegex: string,
136 field: string,
c1e791ba 137 maxSize: number | null,
ac81d1a0
C
138 optional = false
139) {
140 // Should have files
141 if (!files) return optional
142 if (isArray(files)) return optional
143
144 // Should have a file
a1587156 145 const fileArray = files[field]
ac81d1a0
C
146 if (!fileArray || fileArray.length === 0) {
147 return optional
148 }
149
150 // The file should exist
a1587156 151 const file = fileArray[0]
ac81d1a0
C
152 if (!file || !file.originalname) return false
153
0c237b19 154 // Check size
c1e791ba 155 if ((maxSize !== null) && file.size > maxSize) return false
0c237b19 156
ac81d1a0
C
157 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
158}
159
e4c55619
C
160// ---------------------------------------------------------------------------
161
65fcc311
C
162export {
163 exists,
09209296 164 isArrayOf,
2f1548fd 165 isNotEmptyIntArray,
72c7248b 166 isArray,
fb719404 167 isIntOrNull,
72c7248b 168 isIdValid,
345da516 169 isSafePath,
72c7248b
C
170 isUUIDValid,
171 isIdOrUUIDValid,
47564bbe 172 isDateValid,
2efd32f6 173 toValueOrNull,
c8861d5d 174 toBooleanOrNull,
ac81d1a0 175 isBooleanValid,
360329cc 176 toIntOrNull,
d525fc39 177 toArray,
f0a39880 178 toIntArray,
f2eb23cd
RK
179 isFileFieldValid,
180 isFileMimeTypeValid,
ac81d1a0 181 isFileValid
65fcc311 182}