litebb/validators/reply.validator.js

1const joi = require("joi"); 2 3const createNewReply = (req, res, next) => { 4 const payload = { 5 content: req.body.content, 6 postId: req.params.post_id, 7 }; 8 9 const schema = joi.object({ 10 content: joi.string().required(), 11 postId: joi.number().required(), 12 }); 13 14 const { error, value } = schema.validate(payload); 15 16 if (error) { 17 console.log(error.details); 18 req.flash( 19 "error", 20 error.details.map((err) => err.message), 21 ); 22 res.render("new_reply", { 23 title: "New Reply", 24 body: req.body, 25 flashes: req.flash(), 26 }); 27 return; 28 } else { 29 req.xop = { 30 content: value.content, 31 postId: value.postId, 32 }; 33 next(); 34 } 35}; 36 37module.exports = { 38 createNewReply, 39}; 40