Home > Enterprise >  Express-Validator "validationResult" returns [object Object]
Express-Validator "validationResult" returns [object Object]

Time:03-09

I'm modifying the Error handling of the Express-Validator middleware to go through my custom Error Handling middleware but the message of the validationResult resolves to [object Object].

Express-Validator error handling

const expressValidationError = (req, res, next) => {
  const errors = validationResult(req);

  if (!errors.isEmpty()) {
    const err = new Error(errors.array());
    err.status = 400;
    next(err);
  } else {
    next();
  }
};

Express Error Handling middleware

app.use((err, req, res, next) => {
  const status = err.status || 500;
  res.status(status).json({
    error: {
      status: status,
      message: err.message || "Internal Server Error",
    },
  });
});

This is the result of the error object which is returned from validationResult:

Result {
  formatter: [Function: formatter],
  errors: [
    {
      value: 'b',
      msg: 'Username Must be Between 5 to 255 Characters Long!',
      param: 'username',
      location: 'body'
    }
  ]
}

And this is the errors.array():

[
  {
    value: 'b',
    msg: 'Username Must be Between 5 to 255 Characters Long!',
    param: 'username',
    location: 'body'
  }
]

I know that I can just use res.status(400).json({ error: errors.array() }); in the Express-Validator and avoid all these but I would prefer if it was passed to next() and handled by the error middleware.

Is there any way to do so or should I just go with the other way?

CodePudding user response:

the problem is not the validator, but that the Error object takes a string parameter as the first parameter, and you're passing it an object (array), hence the object object.

so JSON.stringify the object:

const err = new Error(JSON.stringify(errors.array()));

CodePudding user response:

If you see the below code, this is what is happening. You are passing an array of objects.

let status = new Error([{}]);
console.log(status); // Error: [object Object]

Now, when you check the validation object it is something like this:

// errors will be like
[{ myLocation: 'body' }, { myLocation: 'query' }...]

All you have to make it a string:

const expressValidationError = (req, res, next) => {
  const errors = validationResult(req);

  if (!errors.isEmpty()) {

    // below is only useful when you don't have array of objects
    // const err = new Error(errors.array().toString());

    const err = new Error(errors.array().map(el => el['msg']).toString());
    err.status = 400;
    next(err);
  } else {
    next();
  }
};

Adding which key has what validation error:

const expressValidationError = (req, res, next) => {
  const errors = validationResult(req);

  if (!errors.isEmpty()) {

    // below is only useful when you don't have array of objects
    // const err = new Error(errors.array().toString());

    const err = new Error(errors.array().map(el => `${el[param]} - has Error: ${el['msg']}`).toString());
    err.status = 400;
    next(err);
  } else {
    next();
  }
};
  • Related