针对 Swagger API 模式验证 JSON模式、Swagger、API、JSON

2023-09-07 13:44:14 作者:成全情深不寿

我从一些 JSON 文件创建了一个 API 规范,我正在尝试测试这些文件是否符合 API 规范.

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

有一些很好的工具可以针对 JSON 模式进行验证,但我没有机会找到一个工具来验证在 Swagger 中创建的规范(用于创建 API 模式的工具).我找到的唯一解决方案是在 Swagger-Editor 中生成客户端/服务器,这很麻烦.

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.

是否已有工具可以根据 Swagger Schema 验证 JSON?

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

推荐答案

评论中的 Arnaud 是正确的,这里有两个单独的问题.

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

您是否要验证您的规范是有效的 OpenAPI(fka.Swagger)规范

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

你可以

将您的规范复制到 在线 Swagger 编辑器,它会抛出错误.快速浏览 source 并没有告诉我它使用什么来创建这些错误,但它似乎并没有联系服务器来做这件事......使用官方的 swagger-parser for Java.对 JavaScript(浏览器或节点)使用非官方的 swagger-parser. 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).

或验证此规范的实现是否会生成对您的 JSON 模式有效的 JSON?

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

换句话说,这是来自请求或响应正文的一些 JSON,是否正确?

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

Swagger 的