针对 Swagger API 模式验证 JSON [英] Validating JSON against Swagger API schema

查看:20
本文介绍了针对 Swagger API 模式验证 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一些 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 的 模式对象,它们是实际描述 JSON(而不是端点或元数据)的内容.Swagger 使用 JSON Schema 的子集(缺少:oneOfpatternProperties 等).为此,您可以使用 JSON 模式验证器.这里列出了 37 个;我要向这个也支持 YAML 模式的在线验证器 致敬.

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.

但是,当我说 Swagger 依赖于 JSON API 的一个子集时,我撒了谎.在 Swagger 中有一些具有特殊含义的固定字段不属于 JSON Schema.其中之一是用于多态的discriminator.我不知道可以处理 discriminator 的 Swagger 验证器.有 相当数量的工具 用于 swagger,有些声称可以进行验证,但很多都是废弃软件,适用于旧版本,功能不完整,与其他技术相关,等等.如果我缺少一个成熟且维护良好的库,我很想知道.

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.

这篇关于针对 Swagger API 模式验证 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆