【问题标题】:Does JSON schema have any standards or conventions for associating an instance document with the schema to which it conforms?JSON 模式是否有任何标准或约定将实例文档与其符合的模式相关联?
【发布时间】:2018-10-09 10:16:18
【问题描述】:

我是 Json Schema 的新手,但过去曾使用 xsd(xml 模式)进行过很多工作。
使用 xml 模式,可以在自己的 xml 文档中标记元素 带有“schemaLocation”属性。此属性可由验证 xml 使用 解析器验证元素的内容和结构是否符合其关联的 架构。在 xml 你做这样的事情:

<animal 
    xsi:schemaLocation="
        http://www.zippy.com 
        http://foo.bar.com/animal.xsd">
    type="dog"
    name="rover"/>

我想知道:在 JSON 模式中是否有一些标准方法可以做到这一点?
我在规范中找不到任何内容或 我经历的教程。
我希望可能会有这样的事情:

{
    "schema": "http://foo.bar.com/animal.schema.json",

    "animal": {
        "type": "dog",
        "name": "rover",
    }
}

我的目标是让我的 REST 服务返回的每个 JSON 文档都包含一个注释 (属性“模式”或类似的东西)指向模式 验证该特定实例...然后我可以选择在“验证”中启动该服务 模式',其中它会自动验证出站响应以确保它们符合架构。

非常感谢任何建议。

【问题讨论】:

    标签: json schema jsonschema


    【解决方案1】:

    JSON 文档没有标准方法来识别描述它的架构。 JSON Schema 的设计目标之一是它不会对正在验证的 JSON 文档的结构施加任何影响。

    但是,JSON Schema 确实定义了一种将 JSON Schema 链接到 HTTP 响应上下文中的文档的方法。您可以使用describedby Link 标头来标识架构。

    HTTP/1.1 200 OK
    Content-Type: application/json
    Link: <http://foo.bar.com/animal.schema.json>; rel="describedby"
    
    {
        "animal": {
            "type": "dog",
            "name": "rover"
        }
    }
    

    http://json-schema.org/latest/json-schema-core.html#rfc.section.10.1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 2013-02-20
      • 2023-04-08
      相关资源
      最近更新 更多