aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/server.ts
blob: a491dfeb33e62e50d746467d9291a6e0afda984b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as express from 'express'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
import { isHostValid } from '../../helpers/custom-validators/servers'
import { ServerModel } from '../../models/server/server'
import { body } from 'express-validator/check'

const serverGetValidator = [
  body('host').custom(isHostValid).withMessage('Should have a valid host'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking serverGetValidator parameters', { parameters: req.body })

    if (areValidationErrors(req, res)) return

    const server = await ServerModel.loadByHost(req.body.host)
    if (!server) {
      return res.status(404)
         .send({ error: 'Server host not found.' })
         .end()
    }

    res.locals.server = server

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  serverGetValidator
}