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