4

I know that there's a tool that is able to do an online validation:

http://online.swagger.io/validator?url=http://petstore.swagger.io/v2/swagger.json

I'm writing a JUnit test that validates the project's swagger.json file. It's important that this validation can be done offline, because the test runs as localhost, and that validation tool can't reach a localhost server.

So, is it possible to validate a Swagger 2.0 JSON file, offline?

Mario S
  • 1,914
  • 1
  • 19
  • 32
  • 1
    You can download the swagger spec and then maybe build a tool to validate your JSON against it. More details here - http://stackoverflow.com/q/27808804/5934435 – Sampada Mar 14 '16 at 02:50
  • 1
    Download Swagger schema...not swagger spec. The stackoverflow app is not letting me edit the comment, hence correcting in another comment. – Sampada Mar 14 '16 at 02:52
  • 1
    @SampadaWagde Thanks for your advice. I just was researching that possibility. I found the schema on https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v2.0/schema.json. – Mario S Mar 14 '16 at 02:53

3 Answers3

2

I'm very satisfied with this Validator from Atlassian: https://bitbucket.org/atlassian/swagger-request-validator

There is still active development, so I guess they will also provide something for OpenAPI 3.

Dennis Kieselhorst
  • 1,280
  • 1
  • 13
  • 23
1

I have created a Maven project that validates swagger JSON documents if you ever decide to use Maven for running your tests.

You can clone the project here: https://github.com/navidsh/maven.swagger.validator

  • I need to validate on a JUnit test because it will be tested on the CI process, before deployment. Thank you, anyway. – Mario S Mar 14 '16 at 05:07
0

Well, I finished a Swagger validator using fge/json-schema-validator and Jackson. It uses the Swagger 2.0 schema to validate.

https://gist.github.com/mariosotil/e1219d4e946c643fe0e5

@Singleton
public class SwaggerValidator {

    public ArrayNode validate(JsonNode jsonNode) {
        return Optional.of(jsonNode)
                .map(this::validateWithinSwaggerSchema)
                .map(this::getMessagesAsJsonArray)
                .get();
    }

    // [...]
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Mario S
  • 1,914
  • 1
  • 19
  • 32