PeerTube/server/middlewares/validators/account.ts

40 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-11-09 10:51:58 -06:00
import * as express from 'express'
2017-11-13 10:39:41 -06:00
import { param } from 'express-validator/check'
2018-01-03 09:38:50 -06:00
import { isAccountIdExist, isAccountNameValid, isLocalAccountNameExist } from '../../helpers/custom-validators/accounts'
import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
2017-12-28 04:16:08 -06:00
import { logger } from '../../helpers/logger'
2017-11-27 10:30:46 -06:00
import { areValidationErrors } from './utils'
2017-11-09 10:51:58 -06:00
const localAccountValidator = [
2017-11-14 10:31:26 -06:00
param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
2017-11-09 10:51:58 -06:00
2017-11-27 10:30:46 -06:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-11-09 10:51:58 -06:00
logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
2017-11-27 10:30:46 -06:00
if (areValidationErrors(req, res)) return
if (!await isLocalAccountNameExist(req.params.name, res)) return
return next()
2017-11-09 10:51:58 -06:00
}
]
2018-01-03 09:38:50 -06:00
const accountsGetValidator = [
param('id').custom(isIdOrUUIDValid).withMessage('Should have a valid id'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
if (!await isAccountIdExist(req.params.id, res)) return
return next()
}
]
2017-11-09 10:51:58 -06:00
// ---------------------------------------------------------------------------
export {
2018-01-03 09:38:50 -06:00
localAccountValidator,
accountsGetValidator
2017-11-09 10:51:58 -06:00
}