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