apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
702 stars 373 forks source link

A hook or mechanism to add custom validation #467

Open amarzavery opened 7 years ago

amarzavery commented 7 years ago

So far, I have loved this tool. It provides a lot of value. However, it would be better if the tool provides hooks for adding custom validation.

One of the biggest plus point of swagger specification is that it allows people to extend by using vendor extensions "x-*". I strongly believe that this should happen with swagger-tools as well.

I want to validate the ISO 8601 duration which is a format that our tool supports with properties or parameters of "type": "string". Currently I don't see a way to do that.

I don't expect this project to support everything, however, a mechanism to add custom validation would immensely help.

whitlockjc commented 7 years ago

sway already has this and the plan is to build new middleware that replaces swagger-tools using Sway. I'll see if I can think of a good way to backport this feature into swagger-tools.

amarzavery commented 7 years ago

Can sway be used for client side validation? It seems like it is better suited for server side stuff.

whitlockjc commented 7 years ago

It's not bound to either or. In face, swagger-editor uses sway already to do validation in it's client application. Sway was built as a rewrite of the API provided by swagger-tools and is built for use anywhere.

farrago commented 7 years ago

How hard would it be (is it?) to replace the JSON validator inside swagger-tools with a pre-created one when constructing the middleware. This would then allow creating a validator with custom validation built-in while still getting the benefits of the rest of swagger-tools.

This was something I was intending to look into in the new year as we use ajv elsewhere in our code with some custom validation that would be really nice to be able to reuse.

Any thoughts on whether this is doable (or already possible)?

whitlockjc commented 7 years ago

This has been asked as well. I don't have an answer for this right now.

farrago commented 7 years ago

Ok, I'll try and have a look at it sometime early in the new year. If I come up with something that looks reasonable I'll submit a pull request, and you can let me know what you think.

farrago commented 7 years ago

As you can see, I've added a pull request with a proposal to fix most of this. It's limited to just schema objects (and e.g. not path or query parameters), but that's all that is defined as supporting JSON Schema anyway.

Happy to get comments on any changes or improvements that are necessary.