根据Swagger API模式验证JSON [英] Validating JSON against Swagger API schema

查看:388
本文介绍了根据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 Schema进行验证,但是我没有找到针对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编辑器,它将引发错误.快速浏览并不能告诉我它是如何创建这些错误的,但似乎并没有联系服务器来完成它...
  • 使用Java的官方 swagger-parser .
  • 将非官方的 swagger-parser 用于JavaScript(浏览器或Node).
  • 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的 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验证器.有大量的大量工具用于招摇,有些人声称可以进行验证,但其中许多已经被弃用,适用于与其他技术相关联的旧版本(功能不完整).如果我缺少一个成熟且维护良好的图书馆,我很想知道.

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天全站免登陆