PeerTube/server/core/middlewares/validators/pagination.ts

31 lines
880 B
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import express from 'express'
2019-07-25 16:23:44 +02:00
import { query } from 'express-validator'
import { PAGINATION } from '@server/initializers/constants.js'
import { areValidationErrors } from './shared/index.js'
2021-03-09 09:22:05 +01:00
const paginationValidator = paginationValidatorBuilder()
2021-03-09 09:22:05 +01:00
function paginationValidatorBuilder (tags: string[] = []) {
return [
query('start')
.optional()
.isInt({ min: 0 }),
2021-03-09 09:22:05 +01:00
query('count')
.optional()
.isInt({ min: 0, max: PAGINATION.GLOBAL.COUNT.MAX }).withMessage(`Should have a number count (max: ${PAGINATION.GLOBAL.COUNT.MAX})`),
2021-03-09 09:22:05 +01:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
2022-08-17 14:58:40 +02:00
if (areValidationErrors(req, res, { tags })) return
2021-03-09 09:22:05 +01:00
return next()
}
]
}
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
2021-03-09 09:22:05 +01:00
paginationValidator,
paginationValidatorBuilder
2017-05-15 22:22:03 +02:00
}