]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/pagination.ts
Add max lives limit
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pagination.ts
index cca8295ffddc731bc309c3e189fb58210b9fffb0..1cae7848c400fe14f40392f5152c7e96a2097933 100644 (file)
@@ -1,17 +1,25 @@
-import 'express-validator'
 import * as express from 'express'
+import { query } from 'express-validator'
+import { logger } from '../../helpers/logger'
+import { areValidationErrors } from './utils'
+import { PAGINATION } from '@server/initializers/constants'
 
-import { checkErrors } from './utils'
-import { logger } from '../../helpers'
+const paginationValidator = [
+  query('start')
+    .optional()
+    .isInt({ min: 0 }).withMessage('Should have a number start'),
+  query('count')
+    .optional()
+    .isInt({ min: 0, max: PAGINATION.GLOBAL.COUNT.MAX }).withMessage(`Should have a number count (max: ${PAGINATION.GLOBAL.COUNT.MAX})`),
 
-function paginationValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
-  req.checkQuery('start', 'Should have a number start').optional().isInt()
-  req.checkQuery('count', 'Should have a number count').optional().isInt()
+  (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking pagination parameters', { parameters: req.query })
 
-  logger.debug('Checking pagination parameters', { parameters: req.query })
+    if (areValidationErrors(req, res)) return
 
-  checkErrors(req, res, next)
-}
+    return next()
+  }
+]
 
 // ---------------------------------------------------------------------------