swagger-tools
swagger-tools copied to clipboard
Allow custom JSON Schema validator in validator middleware (fixes #467)
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 andvendor extensions (also known ascustom 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
I'll give this a peek.
Thanks, I'll look forward to your comments.
Any chance this could get into the next release?
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).
Ahh, bummer. Ok, thanks for the explanation.