Swagger 2.0 offline validation

戏子无情 提交于 2019-11-30 17:20:18

问题


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?


回答1:


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.




回答2:


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




回答3:


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();
    }

    // [...]
}

Hope this helps.



来源:https://stackoverflow.com/questions/35978671/swagger-2-0-offline-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!