]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/misc.ts
Disable openapi generation
[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
b033851f
C
26function isNotEmptyStringArray (value: any) {
27 return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0
28}
29
09209296
C
30function isArrayOf (value: any, validator: (value: any) => boolean) {
31 return isArray(value) && value.every(v => validator(v))
32}
33
72c7248b
C
34function isDateValid (value: string) {
35 return exists(value) && validator.isISO8601(value)
36}
37
38function isIdValid (value: string) {
39 return exists(value) && validator.isInt('' + value)
40}
41
42function isUUIDValid (value: string) {
43 return exists(value) && validator.isUUID('' + value, 4)
44}
45
fbd67e7f
C
46function areUUIDsValid (values: string[]) {
47 return isArray(values) && values.every(v => isUUIDValid(v))
48}
49
72c7248b
C
50function isIdOrUUIDValid (value: string) {
51 return isIdValid(value) || isUUIDValid(value)
52}
53
360329cc 54function isBooleanValid (value: any) {
47564bbe
C
55 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
56}
57
fb719404
C
58function isIntOrNull (value: any) {
59 return value === null || validator.isInt('' + value)
60}
61
d4a8e7a6 62// ---------------------------------------------------------------------------
f0a39880 63
f2eb23cd
RK
64function isFileFieldValid (
65 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
66 field: string,
67 optional = false
68) {
69 // Should have files
70 if (!files) return optional
71 if (isArray(files)) return optional
72
73 // Should have a file
74 const fileArray = files[field]
75 if (!fileArray || fileArray.length === 0) {
76 return optional
77 }
78
79 // The file should exist
80 const file = fileArray[0]
81 if (!file || !file.originalname) return false
82 return file
83}
84
85function isFileMimeTypeValid (
f6d6e7f8 86 files: UploadFilesForCheck,
f2eb23cd
RK
87 mimeTypeRegex: string,
88 field: string,
89 optional = false
90) {
91 // Should have files
92 if (!files) return optional
93 if (isArray(files)) return optional
94
95 // Should have a file
96 const fileArray = files[field]
97 if (!fileArray || fileArray.length === 0) {
98 return optional
99 }
100
101 // The file should exist
102 const file = fileArray[0]
103 if (!file || !file.originalname) return false
104
105 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
106}
107
ac81d1a0
C
108function isFileValid (
109 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
110 mimeTypeRegex: string,
111 field: string,
c1e791ba 112 maxSize: number | null,
ac81d1a0
C
113 optional = false
114) {
115 // Should have files
116 if (!files) return optional
117 if (isArray(files)) return optional
118
119 // Should have a file
a1587156 120 const fileArray = files[field]
ac81d1a0
C
121 if (!fileArray || fileArray.length === 0) {
122 return optional
123 }
124
125 // The file should exist
a1587156 126 const file = fileArray[0]
ac81d1a0
C
127 if (!file || !file.originalname) return false
128
0c237b19 129 // Check size
c1e791ba 130 if ((maxSize !== null) && file.size > maxSize) return false
0c237b19 131
ac81d1a0
C
132 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
133}
134
e4c55619
C
135// ---------------------------------------------------------------------------
136
d4a8e7a6
C
137function toCompleteUUID (value: string) {
138 if (isShortUUID(value)) return shortToUUID(value)
139
140 return value
141}
142
fbd67e7f
C
143function toCompleteUUIDs (values: string[]) {
144 return values.map(v => toCompleteUUID(v))
145}
146
d4a8e7a6
C
147function toIntOrNull (value: string) {
148 const v = toValueOrNull(value)
149
150 if (v === null || v === undefined) return v
151 if (typeof v === 'number') return v
152
153 return validator.toInt('' + v)
154}
155
156function toBooleanOrNull (value: any) {
157 const v = toValueOrNull(value)
158
159 if (v === null || v === undefined) return v
160 if (typeof v === 'boolean') return v
161
162 return validator.toBoolean('' + v)
163}
164
165function toValueOrNull (value: string) {
166 if (value === 'null') return null
167
168 return value
169}
170
171function toArray (value: any) {
172 if (value && isArray(value) === false) return [ value ]
173
174 return value
175}
176
177function toIntArray (value: any) {
178 if (!value) return []
179 if (isArray(value) === false) return [ validator.toInt(value) ]
180
181 return value.map(v => validator.toInt(v))
182}
183
184// ---------------------------------------------------------------------------
185
65fcc311
C
186export {
187 exists,
09209296 188 isArrayOf,
2f1548fd 189 isNotEmptyIntArray,
72c7248b 190 isArray,
fb719404 191 isIntOrNull,
72c7248b 192 isIdValid,
345da516 193 isSafePath,
b033851f 194 isNotEmptyStringArray,
72c7248b 195 isUUIDValid,
fbd67e7f 196 toCompleteUUIDs,
d4a8e7a6 197 toCompleteUUID,
72c7248b 198 isIdOrUUIDValid,
47564bbe 199 isDateValid,
2efd32f6 200 toValueOrNull,
c8861d5d 201 toBooleanOrNull,
ac81d1a0 202 isBooleanValid,
360329cc 203 toIntOrNull,
fbd67e7f 204 areUUIDsValid,
d525fc39 205 toArray,
f0a39880 206 toIntArray,
f2eb23cd
RK
207 isFileFieldValid,
208 isFileMimeTypeValid,
ac81d1a0 209 isFileValid
65fcc311 210}