]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/misc.ts
Move controller in dedicated functions
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
CommitLineData
ac81d1a0 1import 'multer'
72c7248b 2import * as validator from 'validator'
345da516 3import { sep } from 'path'
c8861d5d 4import toBoolean = require('validator/lib/toBoolean')
fdbda9e3 5
69818c93 6function exists (value: any) {
e4c55619
C
7 return value !== undefined && value !== null
8}
9
345da516
C
10function isSafePath (p: string) {
11 return exists(p) &&
12 (p + '').split(sep).every(part => {
f023a19c 13 return [ '..' ].includes(part) === false
345da516
C
14 })
15}
16
69818c93 17function isArray (value: any) {
e4c55619
C
18 return Array.isArray(value)
19}
20
2f1548fd
C
21function isNotEmptyIntArray (value: any) {
22 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
cef534ed
C
23}
24
09209296
C
25function isArrayOf (value: any, validator: (value: any) => boolean) {
26 return isArray(value) && value.every(v => validator(v))
27}
28
72c7248b
C
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
360329cc 45function isBooleanValid (value: any) {
47564bbe
C
46 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
47}
48
360329cc 49function toIntOrNull (value: string) {
c8861d5d
C
50 const v = toValueOrNull(value)
51
52 if (v === null || v === undefined) return v
53 if (typeof v === 'number') return v
54
55 return validator.toInt(v)
56}
57
58function toBooleanOrNull (value: any) {
59 const v = toValueOrNull(value)
60
61 if (v === null || v === undefined) return v
62 if (typeof v === 'boolean') return v
360329cc 63
c8861d5d 64 return toBoolean(v)
360329cc
C
65}
66
2efd32f6 67function toValueOrNull (value: string) {
360329cc
C
68 if (value === 'null') return null
69
70 return value
71}
72
f0a39880 73function toArray (value: any) {
d525fc39
C
74 if (value && isArray(value) === false) return [ value ]
75
76 return value
77}
78
f0a39880
C
79function toIntArray (value: any) {
80 if (!value) return []
81 if (isArray(value) === false) return [ validator.toInt(value) ]
82
83 return value.map(v => validator.toInt(v))
84}
85
ac81d1a0
C
86function isFileValid (
87 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
88 mimeTypeRegex: string,
89 field: string,
c1e791ba 90 maxSize: number | null,
ac81d1a0
C
91 optional = false
92) {
93 // Should have files
94 if (!files) return optional
95 if (isArray(files)) return optional
96
97 // Should have a file
98 const fileArray = files[ field ]
99 if (!fileArray || fileArray.length === 0) {
100 return optional
101 }
102
103 // The file should exist
104 const file = fileArray[ 0 ]
105 if (!file || !file.originalname) return false
106
0c237b19 107 // Check size
c1e791ba 108 if ((maxSize !== null) && file.size > maxSize) return false
0c237b19 109
ac81d1a0
C
110 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
111}
112
e4c55619
C
113// ---------------------------------------------------------------------------
114
65fcc311
C
115export {
116 exists,
09209296 117 isArrayOf,
2f1548fd 118 isNotEmptyIntArray,
72c7248b
C
119 isArray,
120 isIdValid,
345da516 121 isSafePath,
72c7248b
C
122 isUUIDValid,
123 isIdOrUUIDValid,
47564bbe 124 isDateValid,
2efd32f6 125 toValueOrNull,
c8861d5d 126 toBooleanOrNull,
ac81d1a0 127 isBooleanValid,
360329cc 128 toIntOrNull,
d525fc39 129 toArray,
f0a39880 130 toIntArray,
ac81d1a0 131 isFileValid
65fcc311 132}