swagger-tools icon indicating copy to clipboard operation
swagger-tools copied to clipboard

Return all validation errors, instead of just the first one

Open danielsiwiec opened this issue 8 years ago • 11 comments

It would be very useful, if there was a way to instruct the swagger validator to return all validation errors, instead of just the first occurance. For example:

With a following schema:

/search:
  get:
    parameters:
      - name: firstName
        in: query
        required: true
        type: string
      - name: lastName
        in: query
        type: string
        required: true

if a call is made to /search without any query parameters, I'd like the validator to identify both firstName and lastName as missing, as opposed to only firstName.

Would there be an appetite for such feature? I'd be happy to take a stab at a PR.

danielsiwiec avatar Dec 22 '16 13:12 danielsiwiec

This is a great idea. We already do this when validating the spec but we do not do it when we manually validate a schema.

whitlockjc avatar Dec 22 '16 18:12 whitlockjc

Do I understand correctly, that the spec validation process you referred to applies to loading the definitions (as during the application start up) as opposed to request/response validation (as in with the validator middleware)?

danielsiwiec avatar Dec 22 '16 19:12 danielsiwiec

Validating the spec validates that your Swagger document is valid itself. When the middleware does request/response validation, it uses a similar approach but not the exact same logic. I think the reason this is happening is because we're throwing an Error to indicate that a parameter failed validation (to quick fail) instead of always validating all parameters and returning the array of failures.

whitlockjc avatar Dec 22 '16 19:12 whitlockjc

When I rewrote the API in sway, you'll see that it handles this as you'd expect by validating every parameter regardless of whether a previous parameter had failed. In swagger-tools, you'll see that we do not do this as a failure validating a parameter throws an Error and we no longer process the remaining parameters.

This can be fixed pretty easily.

whitlockjc avatar Dec 22 '16 19:12 whitlockjc

I'd love to see this feature added. Would you like to see a pull request for this or rather write it yourself given you have a good idea of how to do it?

danielsiwiec avatar Dec 23 '16 15:12 danielsiwiec

I could do it pretty quickly but a PR would expedite things.

whitlockjc avatar Dec 24 '16 20:12 whitlockjc

Hi, I'm also interested in this feature. Any news on it ?

gregLibert avatar May 16 '17 10:05 gregLibert

I've not made much progress on it. I rewrote json-refs and I'm in the process of getting swagger-tools and sway updated to the latest json-refs first. Then I'll fix bugs and cut releases.

whitlockjc avatar May 16 '17 16:05 whitlockjc

Does a PR make any sense or will it be a waste of time due to swagger-tools and sway update ?

gregLibert avatar May 16 '17 16:05 gregLibert

PRs are always welcome. The changes I need to make for json-refs won't touch this code so it would be without conflict. My typical release process is this:

  1. Update dependencies
  2. Fix bugs (within reason)
  3. Cut release

whitlockjc avatar May 16 '17 16:05 whitlockjc

Hi, Any update on this feature? I really want this feature with swagger-tools.

aswini1988 avatar Sep 28 '18 14:09 aswini1988