I am using express validator to validate the json body.
router
router.post('/login', [
body('mobno')
.exists().withMessage('Required')
.isLength({ min: 10, max: 12 }).withMessage('10-12 characters')
.isNumeric().withMessage('Numeric'),
body('password')
.not().isEmpty().withMessage('Password is required'),
], async (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.json({ errors: errors.array(), success: false, msg: 'Check Parameters' });
}
// do stuff and respond
});
response
{
"errors": [
{
"value": "998716***658()",
"msg": "10-12 characters",
"param": "mobno",
"location": "body"
},
{
"value": "998716***658()",
"msg": "Numeric",
"param": "mobno",
"location": "body"
}
],
"success": false,
"msg": "Check Parameters"
}
In the frontend, I am using Vuetify, hence I need the resonse to be in a format which can easily be consumed by the frontend.
Expected output
{
"errors": {
"mobno": [
"10-12 characters",
"Numeric"
]
},
"success": false,
"msg": "Check Parameters"
}
Question
- Is there any option/function which I can hook into which will format the
errorsin the way I want it. - I am thinking to use Lodash for this transformation, any suggestions on how can this be achieved?