Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate array of strings with joi, must be strings

I need to validate an array to check if it's elements are strings using joi. It always sends the error of "Inavlid tag".

// returned array from req.body
let tags = ["Vue", "React", "Angular"]

// joi shema
const schema = {
     tags: Joi.array().items(Joi.string()),
};

const { error, value } = Joi.validate(tags, schema);

if (error) {
     return res.status(400).send({ tagError: "Invalid tag" });
}
like image 884
ImInYourCode Avatar asked May 30 '19 13:05

ImInYourCode


1 Answers

Joi was recently changed to @hapi/joi (literally 2 weeks ago), so make sure first and foremost that you've switched out the NPM package properly:npm uninstall joi and npm i -s @hapi/joi. Make sure to change your require statements for this change, also.

To define your schema in this new package, you would use:

const schema = Joi.array().items(Joi.string());
like image 111
Len Joseph Avatar answered Sep 28 '22 06:09

Len Joseph