]>
Commit | Line | Data |
---|---|---|
e4f97bab | 1 | import * as express from 'express' |
7a7724e6 | 2 | import { param } from 'express-validator/check' |
e8cb4409 C |
3 | import { |
4 | isAccountIdExist, | |
d14a9532 | 5 | isAccountIdValid, |
e8cb4409 C |
6 | isAccountNameValid, |
7 | isAccountNameWithHostExist, | |
8 | isLocalAccountNameExist | |
9 | } from '../../helpers/custom-validators/accounts' | |
da854ddd | 10 | import { logger } from '../../helpers/logger' |
a2431b7d | 11 | import { areValidationErrors } from './utils' |
d14a9532 | 12 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
e4f97bab C |
13 | |
14 | const localAccountValidator = [ | |
350e31d6 | 15 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), |
e4f97bab | 16 | |
a2431b7d | 17 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
e4f97bab C |
18 | logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) |
19 | ||
a2431b7d C |
20 | if (areValidationErrors(req, res)) return |
21 | if (!await isLocalAccountNameExist(req.params.name, res)) return | |
22 | ||
23 | return next() | |
e4f97bab C |
24 | } |
25 | ] | |
26 | ||
265ba139 | 27 | const accountsGetValidator = [ |
d14a9532 | 28 | param('id').custom(isAccountIdValid).withMessage('Should have a valid id/uuid/name/name with host'), |
265ba139 C |
29 | |
30 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
31 | logger.debug('Checking accountsGetValidator parameters', { parameters: req.params }) | |
32 | ||
33 | if (areValidationErrors(req, res)) return | |
d14a9532 C |
34 | |
35 | let accountFetched = false | |
36 | if (isIdOrUUIDValid(req.params.id)) accountFetched = await isAccountIdExist(req.params.id, res, false) | |
37 | if (!accountFetched) accountFetched = await isAccountNameWithHostExist(req.params.id, res, true) | |
38 | ||
39 | if (!accountFetched) return | |
265ba139 C |
40 | |
41 | return next() | |
42 | } | |
43 | ] | |
44 | ||
e8cb4409 C |
45 | const accountsNameWithHostGetValidator = [ |
46 | param('nameWithHost').exists().withMessage('Should have an account name with host'), | |
47 | ||
48 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
49 | logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params }) | |
50 | ||
51 | if (areValidationErrors(req, res)) return | |
52 | if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return | |
53 | ||
54 | return next() | |
55 | } | |
56 | ] | |
57 | ||
e4f97bab C |
58 | // --------------------------------------------------------------------------- |
59 | ||
60 | export { | |
265ba139 | 61 | localAccountValidator, |
e8cb4409 C |
62 | accountsGetValidator, |
63 | accountsNameWithHostGetValidator | |
e4f97bab | 64 | } |