I am new to Joi. I try to put some rules to signup users. But every new user could signup and "if" command is always executed.
const Joi = require ('joi')
const authSchema = Joi.object().keys({
email:Joi.string().email().required(),
password:Joi.string().min(10).max(24).required()
})
exports.email = (req,res,next) => {
if(authSchema.validate(req.body)){
console.log("je suis dans le middleware joi")
console.log("-------> req.body")
console.log(req.body)
// authSchema.validate({email: req.body.email, password: req.body.password})
// authSchema.validate(req.body)
next()
} else{
res.status(400).json({ error: "erreur de data validataion" });
console.log("erreur ne convient pas a joi")
}
}
Hey use this is how i do main
validateInput(rowData) {
const schema = Joi.object().keys({
email: Joi.string().email().required(),
firstName: Joi.string().required(),
lastName: Joi.string().required(),
contact: Joi.string().required(),
roleId: Joi.number().allow("", null),
});
return schema.validate(rowData);
},
And call it like this
const { error } = await validateInput(user); // request body user.
if (error) {
return res.status(400).send({ message: error.details[0].message });
// it will return for you the message.
}
Here is a link on how you can use Joi as a middleware. https://dev.to/tayfunakgc/middleware-based-joi-validation-in-expressjs-2po5