Request validation against swagger specification

I have a requirement to validate requests against a swagger definition (preferably json file) . Let us know if there is something that does this already. Please note that we're not using node.js

Solved Solved
0 9 7,163
1 ACCEPTED SOLUTION

Former Community Member
Not applicable

Implementations like TV4 implement json schema specification (draft v4). However, Open API specification does not fully implement the json schema specification.

I'd say you are probably better off with swagger-tools (https://www.npmjs.com/package/swagger-tools) which also has a standalone implementation that can work with the JavaScript policy. swagger-tools will work directly off the Open API spec and won't require you to create json schemas.

View solution in original post

9 REPLIES 9

While there's nothing built in the platform, it does allow you to use outside libraries to do the validation.

For example, if you want to validate payloads against their schema, you can use something like TV4 (http://geraintluff.github.io/tv4/) and have a javascript policy doing the validation. In this case, the schemas would be the same ones as defined in the swagger/openapi definition.

Thanks @Ricardo de Andrade . As per Srinandan, open API spec doesn't confirm fully to JSON schema.

Former Community Member
Not applicable

Implementations like TV4 implement json schema specification (draft v4). However, Open API specification does not fully implement the json schema specification.

I'd say you are probably better off with swagger-tools (https://www.npmjs.com/package/swagger-tools) which also has a standalone implementation that can work with the JavaScript policy. swagger-tools will work directly off the Open API spec and won't require you to create json schemas.

@Srinandan Sridhar What standalone implementation it has which can work with JavaScript policy? Didn't get this as this is a node js module. Kindly elaborate?

Former Community Member
Not applicable

@Sunandita Dam try this link https://raw.github.com/apigee-127/swagger-tools/master/browser/swagger-tools-standalone-min.js

When you want to use validation in the JavaScript policy, refer to JS file above as a IncludeURL

@srinandans@google This is working only for swagger 1.2 and 2.0 but not for open api 3.0. Could you please suggest javascript library which supports all versions.

My requirement is to create an utility which should be able to validate requests against swagger spec but don't think swagger-tools is very useful there as key purpose of such projects are to create APIs based on swagger-spec and validation is one part of it.

I recently did the same and created an article on how to use the OpenAPI Spec to validate a JSON request, using a single Javascript policy and tv4, for all the endpoints in a proxy. Check it out here: https://community.apigee.com/articles/42993/using-the-openapi-spec-to-validate-json-requests.html

I provided an updated solution that supports OAS 2 and 3 request validation, plus it validates required headers and query params. See: https://community.apigee.com/articles/88441/validate-json-requests-using-openapi-spec-20-or-30.html