I have endpoint where I post some data to create a user. I am validating the request body with Joi. But when I post using Postman, validation fails.
Here, something similar to the validation function,
const checkData = (data) => {
const schema = Joi.object({
image: Joi.string(),
greeting: Joi.string().valid("Hello World!", "Welcome").required(),
email: Joi.string().email().required(),
});
return schema.validateAsync(data);
};
Here is the multer setup,
const storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, "./upload/");
},
filename: (req, file, callback) => {
callback(null, Date.now().toString() + "_" + file.originalname);
},
});
const fileFilter = (req, file, callback) => {
if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") {
callback(null, true);
} else {
callback(null, false);
}
};
const upload = multer({ storage, fileFilter });
And, here is my route,
router.post("/create/:whatever", upload.single("image"), async (req, res) => {
try {
await checkData(req.body);
res.send(req.params.whatever);
} catch (err) {
res.send({ message: err.details[0].message });
}
});
And the error response I got saying,
{
"message": "\"email\" must be a valid email"
}
In case you are wondering whether I am sending the email right or not, I am, sending an actual valid email. I am sure missing something here, please let me know.