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

Allow custom JSON Schema validator in validator middleware (fixes #467)

Open farrago opened this issue 8 years ago • 5 comments
trafficstars

This changeset provides an option to allow a custom JSON schema validator to be passed to the validation middleware. See further discussion in #467.

In general this actually a small change. validateAgainstSchema() in validators.js already supported passing an external validator to it. So this change is mostly just adding another parameter to the options that swagger-validator accepts, and then passing any validator down until it gets to validateAgainstSchema().

There are other minor changes around error handling to be slightly more flexible in the error handling for validation errors, so that the custom validator doesn't have to be exactly identical in response to z-schema (the default validator). If the response is not identical you will still get errors; you just won't get the slightly improved error messages that swagger-tools generates from the z-schema error messages in some corner cases.

This change also includes updated unit tests to check that:

  • with no custom validator provided (the default), the existing tests continue to pass.
  • the tests also pass when provided with suitably configured z-schema and ajv custom validators.
  • the z-schema and ajv custom validators can also validate custom formats and vendor extensions (also known as custom keywords) that are not able to be verified using the default validator.

NOTE: this ONLY allows custom validation of schema objects in the body of a request or response.

NOTE: path and query parameters, and the schema itself will still be validated by the inbuilt validators, irrespective of any validator passed in.

Finally, the Middleware.md docs have been updated with the new capability.

Test Plan:

  • Run all unit tests and ensure they pass

farrago avatar Jan 06 '17 16:01 farrago

I'll give this a peek.

whitlockjc avatar Jan 13 '17 18:01 whitlockjc

Thanks, I'll look forward to your comments.

farrago avatar Jan 16 '17 16:01 farrago

Any chance this could get into the next release?

jschloer avatar Jul 25 '18 15:07 jschloer

This is unlikely. Long story short, implementing this yourself in your own middlewares would be very, very simple and any enhancements to swagger-tools right now is not likely due to deprecation (#335).

whitlockjc avatar Jul 25 '18 16:07 whitlockjc

Ahh, bummer. Ok, thanks for the explanation.

jschloer avatar Jul 25 '18 20:07 jschloer