diff options
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r-- | server/middlewares/validators/sort.ts | 27 | ||||
-rw-r--r-- | server/middlewares/validators/utils.ts | 26 |
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 @@ | |||
1 | import * as express from 'express' | ||
2 | import { query } from 'express-validator/check' | ||
3 | import { logger } from '../../helpers/logger' | ||
4 | import { SORTABLE_COLUMNS } from '../../initializers' | 1 | import { SORTABLE_COLUMNS } from '../../initializers' |
5 | import { areValidationErrors } from './utils' | 2 | import { checkSort, createSortableColumns } from './utils' |
6 | 3 | ||
7 | // Initialize constants here for better performances | 4 | // Initialize constants here for better performances |
8 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) | 5 | const 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 | |||
47 | function 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 | |||
61 | function 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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { validationResult } from 'express-validator/check' | 2 | import { query, validationResult } from 'express-validator/check' |
3 | import { logger } from '../../helpers/logger' | 3 | import { logger } from '../../helpers/logger' |
4 | 4 | ||
5 | function areValidationErrors (req: express.Request, res: express.Response) { | 5 | function 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 | ||
18 | function 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 | |||
32 | function createSortableColumns (sortableColumns: string[]) { | ||
33 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) | ||
34 | |||
35 | return sortableColumns.concat(sortableColumnDesc) | ||
36 | } | ||
37 | |||
18 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
19 | 39 | ||
20 | export { | 40 | export { |
21 | areValidationErrors | 41 | areValidationErrors, |
42 | checkSort, | ||
43 | createSortableColumns | ||
22 | } | 44 | } |