krakenjs / swaggerize-routes

Swagger document driven route builder.
Other
58 stars 57 forks source link

Improve validation error context #90

Open delaguilaluis opened 7 years ago

delaguilaluis commented 7 years ago

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;
{
  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
}
{
  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. 😄