Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating JSON against Swagger API schema

I created an API spec from some JSON files and I am trying to test if the files validate against the API spec.

There are some good tools to validate against JSON Schema, but I did not have chance to find a tool to validate against specs created in the Swagger (tool for creating API schema). The only solution I found is generating a client/server in the Swagger-Editor, it is quite cumbersome.

Is there already an existing tool to validate JSON against Swagger Schema?

like image 881
Peter G. Avatar asked Sep 01 '16 15:09

Peter G.


People also ask

How do you validate a JSON file against a schema?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

Can we validate JSON with schema?

JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements. You can add it to your code as an additional test or in run-time.

Does swagger use JSON Schema?

Swagger supports only subset of JSON Schema Draft 4 Specification of Swagger 1.2 and 2.0 states, it supports only subset of JSON Schema Draft 4 (s. here). This means, that: one cannot rely, that each valid JSON Schema can be completely supported by Swagger.


Video Answer


2 Answers

Arnaud in the comments is correct that there are two separate questions here.

Do you want to validate that your spec is a valid OpenAPI (fka. Swagger) spec

You can

  • Copy your spec to the online Swagger editor and it will throw errors. A quick dive through the source doesn't tell me what it's using to create those errors, but it doesn't seem to be contacting a server to do it...
  • Use the official swagger-parser for Java.
  • Use the unofficial swagger-parser for JavaScript (browser or Node).

or validate that an implementation of this spec would produce JSON which is valid regarding your JSON schemas?

In other words, here's some JSON from a request or response body, is it correct?

Swagger relies on another standard called JSON Schema for its schema objects, which are what actually describes the JSON (rather than endpoints or metadata). Swagger uses a subset of JSON Schema (missing: oneOf, patternProperties, among others). To that end, you can use a JSON Schema validator. There are 37 listed here; I'll give a shoutout to this online validator that also supports YAML schemas.

But, when I said Swagger relies on a subset of JSON API, I lied. There are a handful of fixed fields that have special meaning in Swagger that's not part of JSON Schema. One of them is discriminator which is used for polymorphism. I am not aware of a Swagger validator that can process discriminator. There are a fair number of tools for swagger and some claim to do validations, but many are abandonware, meant for old versions, not feature-complete, tied to other technologies, and so on. If there's a mature and well-maintained library that I'm missing, I'd love to know.

like image 78
mgold Avatar answered Sep 30 '22 08:09

mgold


Atlassian's swagger-request-validator is a Java library that can do such validation:

A Java library for validating request/responses against a OpenAPI / Swagger specification. Includes support for Swagger v2 and OpenAPI v3 specifications and adapters for common mock and testing libraries.

The core library is not tied to any specific HTTP library, but they also provide additional modules that integrate with Spring MVC, MockMVC, REST Assured etc.

There is also swagger-schema-validator that can validate a JSON document against a Swagger V2 definition (disclaimer: I'm the author). This Java library is less complete than Atlassian's though.

like image 38
Bastien Jansen Avatar answered Sep 30 '22 08:09

Bastien Jansen