]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/utils.ts
Agnostic actor image storage
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / utils.ts
index 77a1a0d4bfb4c5a7ded8dec4ccdaecd890fc85a9..4167f6d437f7ca9273abe998d2165ac74966e168 100644 (file)
@@ -1,35 +1,46 @@
-import { validationResult } from 'express-validator/check'
 import * as express from 'express'
+import { query, validationResult } from 'express-validator'
+import { logger } from '../../helpers/logger'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
 
-import { logger } from '../../helpers'
-
-function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction) {
+function areValidationErrors (req: express.Request, res: express.Response) {
   const errors = validationResult(req)
 
   if (!errors.isEmpty()) {
     logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() })
-    return res.status(400).json({ errors: errors.mapped() })
+    res.status(HttpStatusCode.BAD_REQUEST_400)
+       .json({ errors: errors.mapped() })
+
+    return true
   }
 
-  return next()
+  return false
 }
 
-function areValidationErrors (req: express.Request, res: express.Response) {
-  const errors = validationResult(req)
+function checkSort (sortableColumns: string[], tags: string[] = []) {
+  return [
+    query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
 
-  if (!errors.isEmpty()) {
-    logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() })
-    res.status(400).json({ errors: errors.mapped() })
+    (req: express.Request, res: express.Response, next: express.NextFunction) => {
+      logger.debug('Checking sort parameters', { parameters: req.query, tags })
 
-    return true
-  }
+      if (areValidationErrors(req, res)) return
 
-  return false
+      return next()
+    }
+  ]
+}
+
+function createSortableColumns (sortableColumns: string[]) {
+  const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
+
+  return sortableColumns.concat(sortableColumnDesc)
 }
 
 // ---------------------------------------------------------------------------
 
 export {
-  checkErrors,
-  areValidationErrors
+  areValidationErrors,
+  checkSort,
+  createSortableColumns
 }