2017-06-10 15:15:25 -05:00
|
|
|
import * as express from 'express'
|
2019-07-25 09:23:44 -05:00
|
|
|
import { query } from 'express-validator'
|
2020-01-09 02:36:31 -06:00
|
|
|
import { PAGINATION } from '@server/initializers/constants'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { areValidationErrors } from './shared'
|
2016-05-13 11:10:46 -05:00
|
|
|
|
2021-03-09 02:22:05 -06:00
|
|
|
const paginationValidator = paginationValidatorBuilder()
|
2016-05-13 11:10:46 -05:00
|
|
|
|
2021-03-09 02:22:05 -06:00
|
|
|
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})`),
|
2016-05-13 11:10:46 -05:00
|
|
|
|
2021-03-09 02:22:05 -06:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking pagination parameters', { parameters: req.query, tags })
|
2017-11-27 10:30:46 -06:00
|
|
|
|
2021-03-09 02:22:05 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-05-13 11:10:46 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
2021-03-09 02:22:05 -06:00
|
|
|
paginationValidator,
|
|
|
|
paginationValidatorBuilder
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|