swaggerize-routes icon indicating copy to clipboard operation
swaggerize-routes copied to clipboard

Improve validation error context

Open delaguilaluis opened this issue 8 years ago • 0 comments

If a validation error happens, I would like to know the location of the parameter. e.g.header, query, path, body.

Also I would like the context.key to have the right key name instead of 'value'.

The desired result would contain the parameter location under context.location and the right key name under context.key. I've done a local experiment and achieved the desired results by adding this after L129 of lib/validator.js:

detail.context.key = parameter.name;
detail.context.location = parameter.in;
  • Current result:
{
  ValidationError: "Authorization" is required
  // Stacktrace goes here
  name: 'ValidationError',
  details: [
    {
      message: '"Authorization" is required',
      path: 'Authorization',
      type: 'any.required',
      context: {
        key: 'value'
      }
    }
  ],
  _object: undefined,
  annotate: [Function],
  status: 400
}
  • Desired result:
{
  ValidationError: "Authorization" is required
  // Stacktrace goes here
  name: 'ValidationError',
  details: [
    {
      message: '"Authorization" is required',
      path: 'Authorization',
      type: 'any.required',
      context: {
        key: 'Authorization',
        location: 'header'
      }
    }
  ],
  _object: undefined,
  annotate: [Function],
  status: 400
}

Please let me know what do you think. I hope to be making a related PR soon. 😄

delaguilaluis avatar Sep 19 '17 01:09 delaguilaluis