]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/account.ts
ebc2fcf2d89099c72e3effb1f36046a7301fc352
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / account.ts
1 import * as express from 'express'
2 import { param } from 'express-validator/check'
3 import { isAccountIdExist, isAccountNameValid, isLocalAccountNameExist } from '../../helpers/custom-validators/accounts'
4 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
5 import { logger } from '../../helpers/logger'
6 import { areValidationErrors } from './utils'
7
8 const localAccountValidator = [
9 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
10
11 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
13
14 if (areValidationErrors(req, res)) return
15 if (!await isLocalAccountNameExist(req.params.name, res)) return
16
17 return next()
18 }
19 ]
20
21 const accountsGetValidator = [
22 param('id').custom(isIdOrUUIDValid).withMessage('Should have a valid id'),
23
24 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
26
27 if (areValidationErrors(req, res)) return
28 if (!await isAccountIdExist(req.params.id, res)) return
29
30 return next()
31 }
32 ]
33
34 // ---------------------------------------------------------------------------
35
36 export {
37 localAccountValidator,
38 accountsGetValidator
39 }