showing results for - "joi as a middleware"
Stefano
27 Jan 2019
1const Joi = require('joi'); 
2const middleware = (schema, property) => { 
3  return (req, res, next) => { 
4    const { error } = Joi.validate(req[property], schema); 
5    const valid = error == null; 
6    if (valid) { next(); } 
7    else { 
8      const { details } = error; 
9      const message = details.map(i => i.message).join(',')
10      console.log("error", message); 
11      res.status(422).json({ error: message }) 
12    } 
13  } 
14} 
15module.exports = middleware;
16