]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/misc.ts
Add to playlist dropdown
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
CommitLineData
ac81d1a0 1import 'multer'
72c7248b 2import * as validator from 'validator'
fdbda9e3 3
69818c93 4function exists (value: any) {
e4c55619
C
5 return value !== undefined && value !== null
6}
7
69818c93 8function isArray (value: any) {
e4c55619
C
9 return Array.isArray(value)
10}
11
2f1548fd
C
12function isNotEmptyIntArray (value: any) {
13 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
cef534ed
C
14}
15
09209296
C
16function isArrayOf (value: any, validator: (value: any) => boolean) {
17 return isArray(value) && value.every(v => validator(v))
18}
19
72c7248b
C
20function isDateValid (value: string) {
21 return exists(value) && validator.isISO8601(value)
22}
23
24function isIdValid (value: string) {
25 return exists(value) && validator.isInt('' + value)
26}
27
28function isUUIDValid (value: string) {
29 return exists(value) && validator.isUUID('' + value, 4)
30}
31
32function isIdOrUUIDValid (value: string) {
33 return isIdValid(value) || isUUIDValid(value)
34}
35
360329cc 36function isBooleanValid (value: any) {
47564bbe
C
37 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
38}
39
360329cc
C
40function toIntOrNull (value: string) {
41 if (value === 'null') return null
42
43 return validator.toInt(value)
44}
45
2efd32f6 46function toValueOrNull (value: string) {
360329cc
C
47 if (value === 'null') return null
48
49 return value
50}
51
f0a39880 52function toArray (value: any) {
d525fc39
C
53 if (value && isArray(value) === false) return [ value ]
54
55 return value
56}
57
f0a39880
C
58function toIntArray (value: any) {
59 if (!value) return []
60 if (isArray(value) === false) return [ validator.toInt(value) ]
61
62 return value.map(v => validator.toInt(v))
63}
64
ac81d1a0
C
65function isFileValid (
66 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
67 mimeTypeRegex: string,
68 field: string,
c1e791ba 69 maxSize: number | null,
ac81d1a0
C
70 optional = false
71) {
72 // Should have files
73 if (!files) return optional
74 if (isArray(files)) return optional
75
76 // Should have a file
77 const fileArray = files[ field ]
78 if (!fileArray || fileArray.length === 0) {
79 return optional
80 }
81
82 // The file should exist
83 const file = fileArray[ 0 ]
84 if (!file || !file.originalname) return false
85
0c237b19 86 // Check size
c1e791ba 87 if ((maxSize !== null) && file.size > maxSize) return false
0c237b19 88
ac81d1a0
C
89 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
90}
91
e4c55619
C
92// ---------------------------------------------------------------------------
93
65fcc311
C
94export {
95 exists,
09209296 96 isArrayOf,
2f1548fd 97 isNotEmptyIntArray,
72c7248b
C
98 isArray,
99 isIdValid,
100 isUUIDValid,
101 isIdOrUUIDValid,
47564bbe 102 isDateValid,
2efd32f6 103 toValueOrNull,
ac81d1a0 104 isBooleanValid,
360329cc 105 toIntOrNull,
d525fc39 106 toArray,
f0a39880 107 toIntArray,
ac81d1a0 108 isFileValid
65fcc311 109}