diff options
Diffstat (limited to 'server/middlewares/validators/sort.ts')
-rw-r--r-- | server/middlewares/validators/sort.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts new file mode 100644 index 000000000..ebc7333c7 --- /dev/null +++ b/server/middlewares/validators/sort.ts | |||
@@ -0,0 +1,44 @@ | |||
1 | import { checkErrors } from './utils' | ||
2 | import { logger } from '../../helpers' | ||
3 | import { SORTABLE_COLUMNS } from '../../initializers' | ||
4 | |||
5 | // Initialize constants here for better performances | ||
6 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) | ||
7 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) | ||
8 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) | ||
9 | |||
10 | function usersSortValidator (req, res, next) { | ||
11 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) | ||
12 | } | ||
13 | |||
14 | function videoAbusesSortValidator (req, res, next) { | ||
15 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) | ||
16 | } | ||
17 | |||
18 | function videosSortValidator (req, res, next) { | ||
19 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) | ||
20 | } | ||
21 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | export { | ||
25 | usersSortValidator, | ||
26 | videoAbusesSortValidator, | ||
27 | videosSortValidator | ||
28 | } | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | function checkSort (req, res, next, sortableColumns) { | ||
33 | req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) | ||
34 | |||
35 | logger.debug('Checking sort parameters', { parameters: req.query }) | ||
36 | |||
37 | checkErrors(req, res, next) | ||
38 | } | ||
39 | |||
40 | function createSortableColumns (sortableColumns) { | ||
41 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) | ||
42 | |||
43 | return sortableColumns.concat(sortableColumnDesc) | ||
44 | } | ||