Joi version 17+

I’m aware you need to install the same version of the packages to avoid any issues while taking the courses. However, I tried to use the latest version of Joi (17.3.0 at the moment of writing this post) in the Node.js - section 4 - Intro to express - Input validation lesson.

That version is not working since I got a Joi.validate is not a function message. For the sake of curiosity, what would be a valid way to achieve the same result using the latest version of Joi?

1 Like

Hi,

 //  Joi Version -> 17
     app.post("/api/courses", (req, res) => {
        
        const name = { name: req.body.name };

        const schema = Joi.object({ name: Joi.string().min(3).required() });

        const result = schema.validate(name);

        if (result.error) {

          res.status(400).send(result.error.details[0].message);

        }

And the rest would be the same

3 Likes

Thank you @A100D-JS, that did the job! Interesting how Joi now uses object to define the rules. I felt better prepared to use this package now :slight_smile:

Always a pleasure :heart:

How would we write the code for the
function validateCourse(course)?

sorry, figured it out. Here’s the snippet.

function validateCourse(course) {
const schema = Joi.object({
name: Joi.string().min(3).required()
});

return schema.validate(course);

};

1 Like

Hi razahuss
I did tried to return schema.validate(genre) but the result was the same:
TypeError: schema.validate is not a function

Can you help me please ?
Tsk

This piece of code is working for me, hope it helps:

function validateGenre(genre){
const schema = Joi.object({
name: Joi.string().min(3).required()
});
return schema.validate(genre);
}

if you still have that error, check if you required Joi at the start of the file (that was my mistake when I passed all the genre code to a new file inside de router folder)