]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { param, validationResult } from 'express-validator' | |
3 | import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | ||
6 | function areValidationErrors (req: express.Request, res: express.Response) { | |
7 | const errors = validationResult(req) | |
8 | ||
9 | if (!errors.isEmpty()) { | |
10 | logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) | |
11 | res.fail({ | |
12 | message: 'Incorrect request parameters: ' + Object.keys(errors.mapped()).join(', '), | |
13 | instance: req.originalUrl, | |
14 | data: { | |
15 | 'invalid-params': errors.mapped() | |
16 | } | |
17 | }) | |
18 | ||
19 | return true | |
20 | } | |
21 | ||
22 | return false | |
23 | } | |
24 | ||
25 | function isValidVideoIdParam (paramName: string) { | |
26 | return param(paramName) | |
27 | .customSanitizer(toCompleteUUID) | |
28 | .custom(isIdOrUUIDValid).withMessage('Should have a valid video id') | |
29 | } | |
30 | ||
31 | function isValidPlaylistIdParam (paramName: string) { | |
32 | return param(paramName) | |
33 | .customSanitizer(toCompleteUUID) | |
34 | .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id') | |
35 | } | |
36 | ||
37 | // --------------------------------------------------------------------------- | |
38 | ||
39 | export { | |
40 | areValidationErrors, | |
41 | isValidVideoIdParam, | |
42 | isValidPlaylistIdParam | |
43 | } |