2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
2019-07-25 10:23:44 -04:00
|
|
|
import { query } from 'express-validator'
|
2020-01-09 03:36:31 -05:00
|
|
|
import { PAGINATION } from '@server/initializers/constants'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { areValidationErrors } from './shared'
|
2016-05-13 12:10:46 -04:00
|
|
|
|
2021-03-09 03:22:05 -05:00
|
|
|
const paginationValidator = paginationValidatorBuilder()
|
2016-05-13 12:10:46 -04:00
|
|
|
|
2021-03-09 03:22:05 -05:00
|
|
|
function paginationValidatorBuilder (tags: string[] = []) {
|
|
|
|
return [
|
|
|
|
query('start')
|
|
|
|
.optional()
|
2022-08-17 08:27:04 -04:00
|
|
|
.isInt({ min: 0 }),
|
2021-03-09 03:22:05 -05:00
|
|
|
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 12:10:46 -04:00
|
|
|
|
2021-03-09 03:22:05 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2022-08-17 08:58:40 -04:00
|
|
|
if (areValidationErrors(req, res, { tags })) return
|
2021-03-09 03:22:05 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-05-13 12:10:46 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
2021-03-09 03:22:05 -05:00
|
|
|
paginationValidator,
|
|
|
|
paginationValidatorBuilder
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|