【问题标题】:JSONSchema split one big schema file into multiple logical smaller filesJSONSchema 将一个大模式文件拆分为多个逻辑小文件
【发布时间】:2013-08-22 09:14:58
【问题描述】:

我希望将 json 架构的公共部分捕获到一个文件中,然后从主架构文件中引用该文件。所以基本上不是 1 个大的 json 模式文件,而是多个相互引用的文件。我正在使用json-schema-validator lib 进行验证。

例如:

$ ls schemas/
response_schema.json results_schema.json

$ cat schemas/response_schema.json
{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "required": [ "results" ],
    "properties": {
        "results": "####Reference results_schema.json file here somehow####"
    }
}   

$ cat schemas/results_schema.json
{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "array",
    "items": {
        "type": "object",
        "required": ["type", "name"],
        "properties": {
            "name": { "type": "string" },
            "dateOfBirth": { "type": "string" }
        }
    }
}

【问题讨论】:

  • 感谢原子人。挖掘更多发现部分答案。 "results": { "$ref": "file://localhost/c:/eclipse-workspaces/myproject/src/test/resources/schemas/results.json" } 仍然不知道如何给出相对路径!
  • 尝试将其推送到您的服务器,并使用./resources/schemas/results.json 作为参考。我不知道这是否可行,但您至少应该在控制台中看到完整的 url,因为库试图解决它。

标签: json jsonschema


【解决方案1】:

以下解决方案对我有用:

    "results": {
        "$ref": "file:src/test/resources/schemas/results.json"
    }

以上解决方案满足我的要求:

  1. 我所有的架构文件都在本地文件系统上,而不是由某些 url 托管
  2. 指定的路径是相对于我运行 mvn 目标的目录。

【讨论】:

  • 引用没有理由不能是相对的。 :) 它们是相对于当前模式路径(或“id”,如果已定义)进行解析的。
  • 对我来说这根本不起作用。我也尝试了一些替代方案并仔细检查了它们。
【解决方案2】:

这就是我的做法:

给定应用程序根目录的以下文件结构:

/schemas/
         response_schema.json
         results_schema.json

response_schema.json:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "resource:/schemas/response_schema#",
    "type": "object",
    "required": [ "results" ],
    "properties": {
        "results": {
          "type": "object",
           "$ref": "results_schema.json"
     }
}  

results_schema.json:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "resource:/schemas/results_schema#",
    "type": "array",
        "items": {
            "type": "object",
            "required": ["type", "name"],
        "properties": {
            "name": { "type": "string" },
            "dateOfBirth": { "type": "string" }
        }
    }
}

已通过JsonValidator.java 验证

【讨论】:

  • 显示验证是如何完成的,答案不完整,在这种情况下如何使用 JsonValidator 类?
猜你喜欢
  • 2019-11-06
  • 2019-09-23
  • 1970-01-01
  • 2011-12-25
  • 2017-08-21
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多