account.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import * as express from 'express'
  2. import { param } from 'express-validator/check'
  3. import { isAccountNameValid, doesAccountNameWithHostExist, doesLocalAccountNameExist } from '../../helpers/custom-validators/accounts'
  4. import { logger } from '../../helpers/logger'
  5. import { areValidationErrors } from './utils'
  6. const localAccountValidator = [
  7. param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
  8. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  9. logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
  10. if (areValidationErrors(req, res)) return
  11. if (!await doesLocalAccountNameExist(req.params.name, res)) return
  12. return next()
  13. }
  14. ]
  15. const accountNameWithHostGetValidator = [
  16. param('accountName').exists().withMessage('Should have an account name with host'),
  17. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  18. logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params })
  19. if (areValidationErrors(req, res)) return
  20. if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
  21. return next()
  22. }
  23. ]
  24. // ---------------------------------------------------------------------------
  25. export {
  26. localAccountValidator,
  27. accountNameWithHostGetValidator
  28. }