-1

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 ?

TechChain
  • 8,404
  • 29
  • 103
  • 228
  • If it fails validation in the middleware that route handler callback is *never even invoked*. – jonrsharpe May 22 '20 at 07:28
  • 1
    Does this answer your question? [Node.js + Joi how to display a custom error messages?](https://stackoverflow.com/questions/48720942/node-js-joi-how-to-display-a-custom-error-messages) – jonrsharpe May 22 '20 at 07:29
  • Please read the duplicate carefully, those answers do not apply only to the message. – jonrsharpe May 22 '20 at 07:34
  • My question is diffrent joi bypass my response handler and send the reponse directly. I have not control over joi directly sending response – TechChain May 22 '20 at 07:36
  • I'm not sure what you're trying to say now. Yes, using the validation middleware means the response handler isn't called if the payload is invalid; *that's the whole point*. The duplicate shows you how to customise the response that sends. If that's still not enough for you, maybe *don't use the middleware*. – jonrsharpe May 22 '20 at 07:38
  • I am trying to say if the regx pattren don't match then i do not get any call in my route response handler. As you can see router.get response is never called even when regex pattren do not match. Response with json object {message,fileds} is sent. So how can i get a callback so that i can modfiy this object. – TechChain May 22 '20 at 07:41
  • And I'm saying **that is what's supposed to happen**. It seems pointless to continue this discussion. – jonrsharpe May 22 '20 at 07:44
  • Ok i got your point that it will not happen. So what is the best way to validate the params in url with joi validation. I can validate using IF condition but i want to keep validation uniform. – TechChain May 22 '20 at 07:47

1 Answers1

-1

I have found on solution through which i don't have to use validate(errorLogSchema) and can still validate the data.

pass the request params

const reqObject = findErrorLogRequestSchema.validate(req.params);

Define schema

export const findErrorLogRequestSchema = Joi.object({
    id: Joi.string()
    .pattern(new RegExp('^[0-9a-fA-F]{24}$')).message("Id is not valid")
});
TechChain
  • 8,404
  • 29
  • 103
  • 228