This repository was archived by the owner on Aug 4, 2023. It is now read-only.
Allow custom JSON Schema validator in validator middleware (fixes #467)#471
Open
farrago wants to merge 1 commit intoapigee-127:masterfrom
Open
Allow custom JSON Schema validator in validator middleware (fixes #467)#471farrago wants to merge 1 commit intoapigee-127:masterfrom
farrago wants to merge 1 commit intoapigee-127:masterfrom
Conversation
This changeset provides an option to allow a custom schema validator to be passed to the validation middleware. 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](https://github.com/zaggino/z-schema) and [ajv](https://github.com/epoberezkin/ajv) custom validators. - the z-schema and ajv custom validators can also validate custom `format`s and `vendor extension`s (also known as `custom keyword`s) 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
Member
|
I'll give this a peek. |
Author
|
Thanks, I'll look forward to your comments. |
|
Any chance this could get into the next release? |
Member
|
This is unlikely. Long story short, implementing this yourself in your own middlewares would be very, very simple and any enhancements to |
|
Ahh, bummer. Ok, thanks for the explanation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()invalidators.jsalready supported passing an external validator to it. So this change is mostly just adding another parameter to the optionsthat
swagger-validatoraccepts, and then passing any validator down until it gets tovalidateAgainstSchema().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:
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
schemaobjects in the body of a request or response.NOTE:
pathandqueryparameters, and the schema itself will still be validated by the inbuilt validators, irrespective of any validator passed in.Finally, the
Middleware.mddocs have been updated with the new capability.Test Plan: