]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/pagination.ts
Move middleware utils in middlewares
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pagination.ts
index cca8295ffddc731bc309c3e189fb58210b9fffb0..74eae251e04790dbbd53a60b02e7446106995f12 100644 (file)
@@ -1,20 +1,33 @@
-import 'express-validator'
 import * as express from 'express'
+import { query } from 'express-validator'
+import { PAGINATION } from '@server/initializers/constants'
+import { logger } from '../../helpers/logger'
+import { areValidationErrors } from './shared'
 
-import { checkErrors } from './utils'
-import { logger } from '../../helpers'
+const paginationValidator = paginationValidatorBuilder()
 
-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()
+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})`),
 
-  logger.debug('Checking pagination parameters', { parameters: req.query })
+    (req: express.Request, res: express.Response, next: express.NextFunction) => {
+      logger.debug('Checking pagination parameters', { parameters: req.query, tags })
 
-  checkErrors(req, res, next)
+      if (areValidationErrors(req, res)) return
+
+      return next()
+    }
+  ]
 }
 
 // ---------------------------------------------------------------------------
 
 export {
-  paginationValidator
+  paginationValidator,
+  paginationValidatorBuilder
 }