openapi
重点 (Top highlight)
JSON Schema and OpenAPI can seem similar but have different use-cases.
JSON Schema和OpenAPI看起来很相似,但是具有不同的用例。
To begin, how JSON Scheme and OPenAPI differ? Well, in contrast to JSON Schema, an OpenAPI document is a definition for an entire API, not just data models. One might compare JSON Schema with the OpenAPI data model.
首先,JSON方案和OPenAPI有何不同? 好吧,与JSON Schema相反, OpenAPI文档是整个API的定义,而不仅仅是数据模型。 可以将JSON Schema与OpenAPI数据模型进行比较。
为什么需要验证JSON? (Why the need to validate JSON?)
There are a plethora of use-cases, but let me explain why I use it:
有很多用例,但让我解释一下为什么使用它:
Enter the world of Kubernetes and you’ll find yourself surrounded by object manifests which are either defined as YAML or JSON. But having to maintain thousands of such manifests can be a nightmare if your code is repeated. Languages likejsonnet
, a lazy data templating language by Google, let you DRYup (Don’t Repeat Yourself) the configuration code. The jsonnet
spits JSON, offers template reuse, processes a code only if it is required. So not only does it give you a performance boost, it also takes away a big percentage of maintenance.
进入Kubernetes的世界,您会发现自己被对象清单所包围,这些对象清单定义为YAML或JSON。 但是,如果重复执行代码,必须维护数千个这样的清单可能是一场噩梦。 像 jsonnet
,这是Google的一种惰性数据模板语言,可让您DRYup(不要自己重复)配置代码。 的 jsonnet
吐出JSON,提供模板重用,仅在需要时才处理代码。 因此,它不仅可以提高性能,还可以节省大量的维护费用。
Good, now we have a lot of code written in jsonnet
which generates JSON based manifests. Going forward, as custom JSON objects grow, we need some way to validate inputs and auto-document. This is when you would use OpenAPI.
好,现在我们有很多用jsonnet
编写的代码,这些代码生成基于JSON的清单。 展望未来,随着自定义JSON对象的增长,我们需要某种方式来验证输入和自动文档化。 这是您将使用OpenAPI的时间。
JSON模式 (JSON Schema)
JSON schema, as defined at json-schema.org, is a powerful tool for validating the structure of JSON data.
JSON模式 (如 json-schema.org 所定义) 是用于验证JSON数据结构的强大工具。
At its heart, JSON is built on the following data structures:
JSON本质上是基于以下 数据结构构建的 :
object
: for example{ "key1": "value1", "key2": "value2" }
object
:例如{ "key1": "value1", "key2": "value2" }
array
: for example[ "first", "second", "third" ]
array
:例如[ "first", "second", "third" ]
numbers
: for example42
,3.1415926
numbers
:例如42
,3.1415926
string
: for example"This is a string"
string
:例如"This is a string"
boolean
: for exampletrue
andfalse
boolean
:例如true
和false
null
: for examplenull
null
:例如null
These types have analogs in most programming languages, though they may go by different names.
这些类型在大多数编程语言中都有类似物,尽管它们的名称可能不同。
In JSON Schema, an empty object, {}
, is a completely valid schema that will accept any valid JSON (any object, number, string, etc). You can also use true
in place of empty object to represent a schema that matches anything, or false
for a schema that matches nothing.
在JSON模式,一个空对象, {}
完全是一种有效的架构,其将接受任何有效的JSON(任何对象,数字,字符串等)。 您也可以使用