aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/validators/sort.ts27
-rw-r--r--server/middlewares/validators/utils.ts26
2 files changed, 25 insertions, 28 deletions
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index 72c6b34e3..925f47e57 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -1,8 +1,5 @@
1import * as express from 'express'
2import { query } from 'express-validator/check'
3import { logger } from '../../helpers/logger'
4import { SORTABLE_COLUMNS } from '../../initializers' 1import { SORTABLE_COLUMNS } from '../../initializers'
5import { areValidationErrors } from './utils' 2import { checkSort, createSortableColumns } from './utils'
6 3
7// Initialize constants here for better performances 4// Initialize constants here for better performances
8const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) 5const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
@@ -41,25 +38,3 @@ export {
41 jobsSortValidator, 38 jobsSortValidator,
42 videoCommentThreadsSortValidator 39 videoCommentThreadsSortValidator
43} 40}
44
45// ---------------------------------------------------------------------------
46
47function checkSort (sortableColumns: string[]) {
48 return [
49 query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
50
51 (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 logger.debug('Checking sort parameters', { parameters: req.query })
53
54 if (areValidationErrors(req, res)) return
55
56 return next()
57 }
58 ]
59}
60
61function createSortableColumns (sortableColumns: string[]) {
62 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
63
64 return sortableColumns.concat(sortableColumnDesc)
65}
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts
index 55b140103..8f77c9fbd 100644
--- a/server/middlewares/validators/utils.ts
+++ b/server/middlewares/validators/utils.ts
@@ -1,5 +1,5 @@
1import * as express from 'express' 1import * as express from 'express'
2import { validationResult } from 'express-validator/check' 2import { query, validationResult } from 'express-validator/check'
3import { logger } from '../../helpers/logger' 3import { logger } from '../../helpers/logger'
4 4
5function areValidationErrors (req: express.Request, res: express.Response) { 5function areValidationErrors (req: express.Request, res: express.Response) {
@@ -15,8 +15,30 @@ function areValidationErrors (req: express.Request, res: express.Response) {
15 return false 15 return false
16} 16}
17 17
18function checkSort (sortableColumns: string[]) {
19 return [
20 query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
21
22 (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking sort parameters', { parameters: req.query })
24
25 if (areValidationErrors(req, res)) return
26
27 return next()
28 }
29 ]
30}
31
32function createSortableColumns (sortableColumns: string[]) {
33 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
34
35 return sortableColumns.concat(sortableColumnDesc)
36}
37
18// --------------------------------------------------------------------------- 38// ---------------------------------------------------------------------------
19 39
20export { 40export {
21 areValidationErrors 41 areValidationErrors,
42 checkSort,
43 createSortableColumns
22} 44}