How do i fix the code below?

TypeError: Joi.validate is not a function

const Joi = require(“joi”);

module.exports = schema => (req, res, next) => {

const result = Joi.validate(req.body, schema);

if (result.error)

return res.status(400).send({ error: result.error.details[0].message });

next();

};

You’ll probably need to use schema.validate instead of Joi.validate as explained here.

Hi Benbella,

We’re you able to fix the Joi validation code? If so, could you share you code. Mine is still not working. Pls, share your validation.js and listings.js files code. I’ll like to see if there’s something I’m doing wrong.

Thanks,
Yemi

Don’t know if its too late or for the sake future references, Joi no longer supports joi.validate() function.
Solution:
In the backend source code, under routes/listings.js, you convert the schema into a Joi object like this:

const schema = Joi.object({
  title: Joi.string().required(),
   ...
});

Then, under Middleware/validation.js, you change the code from this:

const result = Joi.validate(req.body, schema)

To this:

const result = schema.validate(req.body)