I have route with GET method which accepts a id param in url path. I am validating that id with a regex patteren. I can successfully validate with below code.
const validate = require("express-joi-validate");
router.get("/finderrorlog/:id", validate(errorLogSchema),async (req: Request, res: Response) => {
try {
// Yes, it's a valid ObjectId, proceed with `findById` call.
let result = await errlogsvc.GetErrroLogDetails(req.params.id);
res.json({
status: true,
result: result,
});
} catch (e) {
console.log(e);
logger.error(e);
res.json({ status: false, error: e });
}
});
Schema to validate
export const errorLogSchema = {
params: {
id: Joi.string()
.pattern(new RegExp('^[0-9a-fA-F]{24}$'))
}
}
But here issue is catch
is never executed. I want response like this
{
"status" : false,
"error: " id did not match te pattern"
}
But i get error as
{
"message": "'params.id' with value '5b43c4qfk4e0f07c381392' fails to match the required pattern: /^[0-9a-fA-F]{24}$/",
"field": "params.id"
}
How can i get the custom response as per my requirement ?