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" });
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With