]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/account.ts
Cache AP video route for 5 seconds
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / account.ts
CommitLineData
e4f97bab 1import * as express from 'express'
7a7724e6 2import { param } from 'express-validator/check'
e8cb4409
C
3import {
4 isAccountIdExist,
5 isAccountNameValid,
6 isAccountNameWithHostExist,
7 isLocalAccountNameExist
8} from '../../helpers/custom-validators/accounts'
265ba139 9import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
da854ddd 10import { logger } from '../../helpers/logger'
a2431b7d 11import { areValidationErrors } from './utils'
e4f97bab
C
12
13const localAccountValidator = [
350e31d6 14 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
e4f97bab 15
a2431b7d 16 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
e4f97bab
C
17 logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
18
a2431b7d
C
19 if (areValidationErrors(req, res)) return
20 if (!await isLocalAccountNameExist(req.params.name, res)) return
21
22 return next()
e4f97bab
C
23 }
24]
25
265ba139
C
26const accountsGetValidator = [
27 param('id').custom(isIdOrUUIDValid).withMessage('Should have a valid id'),
28
29 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
31
32 if (areValidationErrors(req, res)) return
33 if (!await isAccountIdExist(req.params.id, res)) return
34
35 return next()
36 }
37]
38
e8cb4409
C
39const accountsNameWithHostGetValidator = [
40 param('nameWithHost').exists().withMessage('Should have an account name with host'),
41
42 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params })
44
45 if (areValidationErrors(req, res)) return
46 if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return
47
48 return next()
49 }
50]
51
e4f97bab
C
52// ---------------------------------------------------------------------------
53
54export {
265ba139 55 localAccountValidator,
e8cb4409
C
56 accountsGetValidator,
57 accountsNameWithHostGetValidator
e4f97bab 58}