Skip to content
This repository has been 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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

farrago
Copy link

@farrago farrago commented Jan 6, 2017

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

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
@whitlockjc
Copy link
Member

I'll give this a peek.

@farrago
Copy link
Author

farrago commented Jan 16, 2017

Thanks, I'll look forward to your comments.

@jschloer
Copy link

Any chance this could get into the next release?

@whitlockjc
Copy link
Member

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).

@jschloer
Copy link

Ahh, bummer. Ok, thanks for the explanation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants