"Joi.validate is not a function", in NODE JS and status code is 500

45 views Asked by At

I use joi for validation in node JS project and I got error while I run it on postman

const Joi = require("joi");
module.exports.saveOrganisationInfo = Joi.object({
  org_id: Joi.number(),
  entity_type: Joi.string(),
  vertical: Joi.string().valid("vertical", "industry"),
  location: Joi.string(),
});

This is my API (post) to post organisation data

organisationRouter.post(
  "/save-organisation-info",
  validationMiddleware(OrganisationValdation),
  OrganisationController.saveOrganisationInfo
);

this is my validation this generates an error message when I run my program. the error message is below

    {
    "code": 500,
    "errors": [
        {
            "type": "UNKNOWN",
            "code": 500,
            "name": "CustomError"
        }
    ],
    "message": "Joi.validate is not a function",
    "result": null,
    "isSuccess": false
}

const Joi = require('joi');
const validationMiddleware = (validationObject, isGet = false) => (req, res, next) => {
  let body;
  if(Object.keys(req.query).length>0){
     body = req.query ? req.query : isGet;
  }else{
    body = req.body ? req.body : isGet;
  }
  const { error } = Joi.validate(body, validationObject);
  if (error) {
    return res
      .status(401)
      .send({ code: 401, message: error.message });
  }
  return next();
};


module.exports = validationMiddleware;
0

There are 0 answers