1 import * as express from 'express'
2 import { param } from 'express-validator/check'
3 import { logger } from '../../helpers'
4 import { isAccountNameValid } from '../../helpers/custom-validators/accounts'
5 import { database as db } from '../../initializers/database'
6 import { AccountInstance } from '../../models'
7 import { checkErrors } from './utils'
9 const localAccountValidator = [
10 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
12 (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
15 checkErrors(req, res, () => {
16 checkLocalAccountExists(req.params.name, res, next)
21 // ---------------------------------------------------------------------------
27 // ---------------------------------------------------------------------------
29 function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) {
30 db.Account.loadLocalByName(name)
33 return res.status(404)
34 .send({ error: 'Account not found' })
38 res.locals.account = account
39 return callback(null, account)
42 logger.error('Error in account request validator.', err)
43 return res.sendStatus(500)