aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-06-10 22:15:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-06-10 22:15:25 +0200
commit69818c9394366b954b6ba3bd697bd9d2b09f2a16 (patch)
treead199a18ec3c322460d6f9523fc383ee562554e0 /server/middlewares/validators
parent4d4e5cd4dca78480ec7f40e747f424cd107376a4 (diff)
downloadPeerTube-69818c9394366b954b6ba3bd697bd9d2b09f2a16.tar.gz
PeerTube-69818c9394366b954b6ba3bd697bd9d2b09f2a16.tar.zst
PeerTube-69818c9394366b954b6ba3bd697bd9d2b09f2a16.zip
Type functions
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/pagination.ts5
-rw-r--r--server/middlewares/validators/pods.ts7
-rw-r--r--server/middlewares/validators/remote/signature.ts5
-rw-r--r--server/middlewares/validators/remote/videos.ts9
-rw-r--r--server/middlewares/validators/sort.ts13
-rw-r--r--server/middlewares/validators/users.ts11
-rw-r--r--server/middlewares/validators/utils.ts5
-rw-r--r--server/middlewares/validators/videos.ts28
8 files changed, 53 insertions, 30 deletions
diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts
index de719c05b..cca8295ff 100644
--- a/server/middlewares/validators/pagination.ts
+++ b/server/middlewares/validators/pagination.ts
@@ -1,7 +1,10 @@
1import 'express-validator'
2import * as express from 'express'
3
1import { checkErrors } from './utils' 4import { checkErrors } from './utils'
2import { logger } from '../../helpers' 5import { logger } from '../../helpers'
3 6
4function paginationValidator (req, res, next) { 7function paginationValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
5 req.checkQuery('start', 'Should have a number start').optional().isInt() 8 req.checkQuery('start', 'Should have a number start').optional().isInt()
6 req.checkQuery('count', 'Should have a number count').optional().isInt() 9 req.checkQuery('count', 'Should have a number count').optional().isInt()
7 10
diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts
index c55a88b85..d8eb90168 100644
--- a/server/middlewares/validators/pods.ts
+++ b/server/middlewares/validators/pods.ts
@@ -1,3 +1,6 @@
1import 'express-validator'
2import * as express from 'express'
3
1import { database as db } from '../../initializers/database' 4import { database as db } from '../../initializers/database'
2import { checkErrors } from './utils' 5import { checkErrors } from './utils'
3import { logger } from '../../helpers' 6import { logger } from '../../helpers'
@@ -5,7 +8,7 @@ import { CONFIG } from '../../initializers'
5import { hasFriends } from '../../lib' 8import { hasFriends } from '../../lib'
6import { isTestInstance } from '../../helpers' 9import { isTestInstance } from '../../helpers'
7 10
8function makeFriendsValidator (req, res, next) { 11function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
9 // Force https if the administrator wants to make friends 12 // Force https if the administrator wants to make friends
10 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { 13 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
11 return res.status(400).send('Cannot make friends with a non HTTPS webserver.') 14 return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
@@ -32,7 +35,7 @@ function makeFriendsValidator (req, res, next) {
32 }) 35 })
33} 36}
34 37
35function podsAddValidator (req, res, next) { 38function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
36 req.checkBody('host', 'Should have a host').isHostValid() 39 req.checkBody('host', 'Should have a host').isHostValid()
37 req.checkBody('email', 'Should have an email').isEmail() 40 req.checkBody('email', 'Should have an email').isEmail()
38 req.checkBody('publicKey', 'Should have a public key').notEmpty() 41 req.checkBody('publicKey', 'Should have a public key').notEmpty()
diff --git a/server/middlewares/validators/remote/signature.ts b/server/middlewares/validators/remote/signature.ts
index 6e3ebe7db..eb5c196eb 100644
--- a/server/middlewares/validators/remote/signature.ts
+++ b/server/middlewares/validators/remote/signature.ts
@@ -1,7 +1,10 @@
1import 'express-validator'
2import * as express from 'express'
3
1import { logger } from '../../../helpers' 4import { logger } from '../../../helpers'
2import { checkErrors } from '../utils' 5import { checkErrors } from '../utils'
3 6
4function signatureValidator (req, res, next) { 7function signatureValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
5 req.checkBody('signature.host', 'Should have a signature host').isURL() 8 req.checkBody('signature.host', 'Should have a signature host').isURL()
6 req.checkBody('signature.signature', 'Should have a signature').notEmpty() 9 req.checkBody('signature.signature', 'Should have a signature').notEmpty()
7 10
diff --git a/server/middlewares/validators/remote/videos.ts b/server/middlewares/validators/remote/videos.ts
index 3380c29e2..2037c0085 100644
--- a/server/middlewares/validators/remote/videos.ts
+++ b/server/middlewares/validators/remote/videos.ts
@@ -1,7 +1,10 @@
1import 'express-validator'
2import * as express from 'express'
3
1import { logger } from '../../../helpers' 4import { logger } from '../../../helpers'
2import { checkErrors } from '../utils' 5import { checkErrors } from '../utils'
3 6
4function remoteVideosValidator (req, res, next) { 7function remoteVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
5 req.checkBody('data').isEachRemoteRequestVideosValid() 8 req.checkBody('data').isEachRemoteRequestVideosValid()
6 9
7 logger.debug('Checking remoteVideos parameters', { parameters: req.body }) 10 logger.debug('Checking remoteVideos parameters', { parameters: req.body })
@@ -9,7 +12,7 @@ function remoteVideosValidator (req, res, next) {
9 checkErrors(req, res, next) 12 checkErrors(req, res, next)
10} 13}
11 14
12function remoteQaduVideosValidator (req, res, next) { 15function remoteQaduVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
13 req.checkBody('data').isEachRemoteRequestVideosQaduValid() 16 req.checkBody('data').isEachRemoteRequestVideosQaduValid()
14 17
15 logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) 18 logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body })
@@ -17,7 +20,7 @@ function remoteQaduVideosValidator (req, res, next) {
17 checkErrors(req, res, next) 20 checkErrors(req, res, next)
18} 21}
19 22
20function remoteEventsVideosValidator (req, res, next) { 23function remoteEventsVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
21 req.checkBody('data').isEachRemoteRequestVideosEventsValid() 24 req.checkBody('data').isEachRemoteRequestVideosEventsValid()
22 25
23 logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) 26 logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body })
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index ebc7333c7..3baee9fb3 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -1,3 +1,6 @@
1import 'express-validator'
2import * as express from 'express'
3
1import { checkErrors } from './utils' 4import { checkErrors } from './utils'
2import { logger } from '../../helpers' 5import { logger } from '../../helpers'
3import { SORTABLE_COLUMNS } from '../../initializers' 6import { SORTABLE_COLUMNS } from '../../initializers'
@@ -7,15 +10,15 @@ const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
7const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) 10const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
8const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) 11const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
9 12
10function usersSortValidator (req, res, next) { 13function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
11 checkSort(req, res, next, SORTABLE_USERS_COLUMNS) 14 checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
12} 15}
13 16
14function videoAbusesSortValidator (req, res, next) { 17function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
15 checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) 18 checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
16} 19}
17 20
18function videosSortValidator (req, res, next) { 21function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
19 checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) 22 checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
20} 23}
21 24
@@ -29,7 +32,7 @@ export {
29 32
30// --------------------------------------------------------------------------- 33// ---------------------------------------------------------------------------
31 34
32function checkSort (req, res, next, sortableColumns) { 35function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) {
33 req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) 36 req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
34 37
35 logger.debug('Checking sort parameters', { parameters: req.query }) 38 logger.debug('Checking sort parameters', { parameters: req.query })
@@ -37,7 +40,7 @@ function checkSort (req, res, next, sortableColumns) {
37 checkErrors(req, res, next) 40 checkErrors(req, res, next)
38} 41}
39 42
40function createSortableColumns (sortableColumns) { 43function createSortableColumns (sortableColumns: string[]) {
41 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) 44 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
42 45
43 return sortableColumns.concat(sortableColumnDesc) 46 return sortableColumns.concat(sortableColumnDesc)
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts
index e0d1d917a..b7b9ef370 100644
--- a/server/middlewares/validators/users.ts
+++ b/server/middlewares/validators/users.ts
@@ -1,8 +1,11 @@
1import 'express-validator'
2import * as express from 'express'
3
1import { database as db } from '../../initializers/database' 4import { database as db } from '../../initializers/database'
2import { checkErrors } from './utils' 5import { checkErrors } from './utils'
3import { logger } from '../../helpers' 6import { logger } from '../../helpers'
4 7
5function usersAddValidator (req, res, next) { 8function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
6 req.checkBody('username', 'Should have a valid username').isUserUsernameValid() 9 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
7 req.checkBody('password', 'Should have a valid password').isUserPasswordValid() 10 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
8 req.checkBody('email', 'Should have a valid email').isEmail() 11 req.checkBody('email', 'Should have a valid email').isEmail()
@@ -23,7 +26,7 @@ function usersAddValidator (req, res, next) {
23 }) 26 })
24} 27}
25 28
26function usersRemoveValidator (req, res, next) { 29function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
27 req.checkParams('id', 'Should have a valid id').notEmpty().isInt() 30 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
28 31
29 logger.debug('Checking usersRemove parameters', { parameters: req.params }) 32 logger.debug('Checking usersRemove parameters', { parameters: req.params })
@@ -44,7 +47,7 @@ function usersRemoveValidator (req, res, next) {
44 }) 47 })
45} 48}
46 49
47function usersUpdateValidator (req, res, next) { 50function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
48 req.checkParams('id', 'Should have a valid id').notEmpty().isInt() 51 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
49 // Add old password verification 52 // Add old password verification
50 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() 53 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
@@ -55,7 +58,7 @@ function usersUpdateValidator (req, res, next) {
55 checkErrors(req, res, next) 58 checkErrors(req, res, next)
56} 59}
57 60
58function usersVideoRatingValidator (req, res, next) { 61function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
59 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) 62 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4)
60 63
61 logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) 64 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts
index 710e65529..0424d5942 100644
--- a/server/middlewares/validators/utils.ts
+++ b/server/middlewares/validators/utils.ts
@@ -1,9 +1,10 @@
1import 'express-validator'
2import * as express from 'express'
1import { inspect } from 'util' 3import { inspect } from 'util'
2 4
3import { logger } from '../../helpers' 5import { logger } from '../../helpers'
4 6
5function checkErrors (req, res, next, statusCode?) { 7function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) {
6 if (statusCode === undefined) statusCode = 400
7 const errors = req.validationErrors() 8 const errors = req.validationErrors()
8 9
9 if (errors) { 10 if (errors) {
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index 47825975c..e99cdefb1 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -1,9 +1,13 @@
1import 'express-validator'
2import * as multer from 'multer'
3import * as express from 'express'
4
1import { database as db } from '../../initializers/database' 5import { database as db } from '../../initializers/database'
2import { checkErrors } from './utils' 6import { checkErrors } from './utils'
3import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' 7import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
4import { logger, isVideoDurationValid } from '../../helpers' 8import { logger, isVideoDurationValid } from '../../helpers'
5 9
6function videosAddValidator (req, res, next) { 10function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
7 req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) 11 req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
8 req.checkBody('name', 'Should have a valid name').isVideoNameValid() 12 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
9 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() 13 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
@@ -27,13 +31,13 @@ function videosAddValidator (req, res, next) {
27 return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') 31 return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
28 } 32 }
29 33
30 videoFile.duration = duration 34 videoFile['duration'] = duration
31 next() 35 next()
32 }) 36 })
33 }) 37 })
34} 38}
35 39
36function videosUpdateValidator (req, res, next) { 40function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
37 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) 41 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
38 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() 42 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
39 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() 43 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
@@ -61,7 +65,7 @@ function videosUpdateValidator (req, res, next) {
61 }) 65 })
62} 66}
63 67
64function videosGetValidator (req, res, next) { 68function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
65 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) 69 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
66 70
67 logger.debug('Checking videosGet parameters', { parameters: req.params }) 71 logger.debug('Checking videosGet parameters', { parameters: req.params })
@@ -71,7 +75,7 @@ function videosGetValidator (req, res, next) {
71 }) 75 })
72} 76}
73 77
74function videosRemoveValidator (req, res, next) { 78function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
75 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) 79 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
76 80
77 logger.debug('Checking videosRemove parameters', { parameters: req.params }) 81 logger.debug('Checking videosRemove parameters', { parameters: req.params })
@@ -88,7 +92,7 @@ function videosRemoveValidator (req, res, next) {
88 }) 92 })
89} 93}
90 94
91function videosSearchValidator (req, res, next) { 95function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
92 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS 96 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
93 req.checkParams('value', 'Should have a valid search').notEmpty() 97 req.checkParams('value', 'Should have a valid search').notEmpty()
94 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) 98 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
@@ -98,7 +102,7 @@ function videosSearchValidator (req, res, next) {
98 checkErrors(req, res, next) 102 checkErrors(req, res, next)
99} 103}
100 104
101function videoAbuseReportValidator (req, res, next) { 105function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
102 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) 106 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
103 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() 107 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
104 108
@@ -109,7 +113,7 @@ function videoAbuseReportValidator (req, res, next) {
109 }) 113 })
110} 114}
111 115
112function videoRateValidator (req, res, next) { 116function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
113 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) 117 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
114 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() 118 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
115 119
@@ -120,7 +124,7 @@ function videoRateValidator (req, res, next) {
120 }) 124 })
121} 125}
122 126
123function videosBlacklistValidator (req, res, next) { 127function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
124 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) 128 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
125 129
126 logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) 130 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
@@ -150,7 +154,7 @@ export {
150 154
151// --------------------------------------------------------------------------- 155// ---------------------------------------------------------------------------
152 156
153function checkVideoExists (id, res, callback) { 157function checkVideoExists (id: string, res: express.Response, callback: () => void) {
154 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { 158 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
155 if (err) { 159 if (err) {
156 logger.error('Error in video request validator.', { error: err }) 160 logger.error('Error in video request validator.', { error: err })
@@ -164,7 +168,7 @@ function checkVideoExists (id, res, callback) {
164 }) 168 })
165} 169}
166 170
167function checkUserCanDeleteVideo (userId, res, callback) { 171function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
168 // Retrieve the user who did the request 172 // Retrieve the user who did the request
169 db.User.loadById(userId, function (err, user) { 173 db.User.loadById(userId, function (err, user) {
170 if (err) { 174 if (err) {
@@ -190,7 +194,7 @@ function checkUserCanDeleteVideo (userId, res, callback) {
190 }) 194 })
191} 195}
192 196
193function checkVideoIsBlacklistable (req, res, callback) { 197function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
194 if (res.locals.video.isOwned() === true) { 198 if (res.locals.video.isOwned() === true) {
195 return res.status(403).send('Cannot blacklist a local video') 199 return res.status(403).send('Cannot blacklist a local video')
196 } 200 }