targetNamespace 的 json 模式等价物是什么?

What is json schema equivalent of targetNamespace?

在任何 xml 文件中,我可以使用 xmlns-attributes 来描述我所指的命名空间。这里描述得很好:What does "xmlns" in XML mean?

然后我可以使用具有目标名称空间的 xml 架构,以便每个人都知道该架构描述了该名称空间。此处有一个问题:Why do we need targetNamespace?

使用 json-schema 我们可以为 json 文档定义模式。我的心理模型是,这大致相当于拥有一个 xsd 文件。

现在,如何在 json 对象中引用架构?我可以使用 $schema 属性引用模式,但是如何声明我自己开发的模式的名称?我不明白 targetNamespace

的等价物

研究编写问题我找到了答案。 targetNamespace 最接近的是 $id 属性。标准状态...

The "$id" keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against. A subschema's "$id" is resolved against the base URI of its parent schema. If no parent sets an explicit base with "$id", the base URI is that of the entire document, as determined per RFC 3986 section 5 [RFC3986].

... 这是 $schema...

前导文本的镜像

The "$schema" keyword is both used as a JSON Schema version identifier and the location of a resource which is itself a JSON Schema, which describes any schema written for this particular version. The value of this keyword MUST be a URI [RFC3986] (containing a scheme) and this URI MUST be normalized. The current schema MUST be valid against the meta-schema identified by this URI.

所以本质上是一样的。 但是,需要注意一些事项:

a) 您在模式中使用 $schema 来定义应该使用什么模式来定义您自己的自定义模式。规范中没有说明任何类型的 object 中的 $schema 应该指示对模式的验证。

b) 您可以在您的模式中定义$schema应该是关于使用什么模式进行验证的指示。

c) 还有其他方法可以指示数据的架构。一个这样的例子是在 http headers 中使用 content-type。另一种是使用 link http headers.

d) vscode 和 visual studio 都将 $schema 解释为对用于验证的模式的引用

该问题已在规范的 github 存储库中讨论。

https://github.com/json-schema/json-schema/issues/235

https://github.com/json-schema/json-schema/issues/220#issuecomment-209452992