diff options
author | Chocobozzz <me@florianbigard.com> | 2018-03-13 11:28:37 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-03-13 11:28:37 +0100 |
commit | 5c328e6610f451d114c30d3a3b4e5d41b3b4b612 (patch) | |
tree | d1b394ca67ccdc6bbbfa51f94ecd4478c8fe76e8 /server/middlewares/validators/utils.ts | |
parent | 0dd46b46e8bf24c5aaeef46709b7910b48daf1ea (diff) | |
download | PeerTube-5c328e6610f451d114c30d3a3b4e5d41b3b4b612.tar.gz PeerTube-5c328e6610f451d114c30d3a3b4e5d41b3b4b612.tar.zst PeerTube-5c328e6610f451d114c30d3a3b4e5d41b3b4b612.zip |
Move sort middleware utils in utils file
Diffstat (limited to 'server/middlewares/validators/utils.ts')
-rw-r--r-- | server/middlewares/validators/utils.ts | 26 |
1 files changed, 24 insertions, 2 deletions
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 | } |