]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/pagination.ts
Give moderators access to edit channels (#4608)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pagination.ts
index de719c05bc42fd7f80c2a414332205c535655e5d..8e4922b9d4fadac10b7747e241793d580219b3e9 100644 (file)
@@ -1,17 +1,33 @@
-import { checkErrors } from './utils'
-import { logger } from '../../helpers'
+import express from 'express'
+import { query } from 'express-validator'
+import { PAGINATION } from '@server/initializers/constants'
+import { logger } from '../../helpers/logger'
+import { areValidationErrors } from './shared'
 
-function paginationValidator (req, res, next) {
-  req.checkQuery('start', 'Should have a number start').optional().isInt()
-  req.checkQuery('count', 'Should have a number count').optional().isInt()
+const paginationValidator = paginationValidatorBuilder()
 
-  logger.debug('Checking pagination parameters', { parameters: req.query })
+function paginationValidatorBuilder (tags: string[] = []) {
+  return [
+    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})`),
 
-  checkErrors(req, res, next)
+    (req: express.Request, res: express.Response, next: express.NextFunction) => {
+      logger.debug('Checking pagination parameters', { parameters: req.query, tags })
+
+      if (areValidationErrors(req, res)) return
+
+      return next()
+    }
+  ]
 }
 
 // ---------------------------------------------------------------------------
 
 export {
-  paginationValidator
+  paginationValidator,
+  paginationValidatorBuilder
 }