swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

Response body validation of mocks

Open CacheControl opened this issue 9 years ago • 3 comments

It appears as though I can set my mocks to be any data structure, and the response will be served without validating against the swagger file's response schema for the path.

For example, if my /users/:id endpoint had the following response schema:

{
  title: 'User',
  description: 'User description.',
  type: 'object',
  properties: {
    userId: {
      description: 'account id',
      type: number
    }
  },
  required: ['userId'],
  additionalProperties: false
};

I can still add the following mocks for the path, and they are served regardless of the fact that the data structure is missing userId, and has additional properties:

let myDB = new MemoryDataStore()
let invalidResponse = { foo: "bar" };
myDB.save(new Resource('/users/1', invalidResponse))

To me, enforcing accurate responses is one of the most important potential value-adds, so this feels like a gap to me. Thoughts on adding a json-validator around the mock middleware, or perhaps I'm missing something?

CacheControl avatar Oct 27 '15 02:10 CacheControl

Sorry for the delayed response. I've been away on holiday for a few weeks.

This feature is definitely on the to-do list. It probably won't be in version 1.0, but hopefully will come soon thereafter. It's little bit tricky, because Express only has request middleware. It doesn't really have a concept of "response middleware", so intercepting the response and validating it against the Swagger response schema isn't totally straightforward. There are a couple ways that I can think of to do it, but they both have pros and cons that will need to be considered.

JamesMessinger avatar Nov 02 '15 03:11 JamesMessinger

+1 - this feature would be great! have not seen a lot of activity on this project in a while though. Has it been abandoned/superseded?

mattfysh avatar Jul 07 '16 19:07 mattfysh

There's a lot of work going on behind the scenes. Doing some big refactoring of the underlying projects that Swagger Express Middleware relies on. I have a day job too though, so my time is limited to nights and weekends, but I assure you that I'm just as eager for new features.

JamesMessinger avatar Jul 07 '16 19:07 JamesMessinger