JSONSchema将一个大的模式文件拆分为多个逻辑较小的文件

时间:2021-01-23 09:26:42

I want the common parts of json schema to be captured in a file and then reference this file from the main schema file. So basically instead of 1 big json schema file, multiple files which reference each other. Am using json-schema-validator lib to validate.

我希望在文件中捕获json模式的公共部分,然后从主模式文件中引用此文件。所以基本上代替了1个大的json模式文件,相互引用的多个文件。我使用json-schema-validator lib来验证。

E.g.:

$ 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" }
        }
    }
}

2 个解决方案

#1


6  

Following solution worked for me:

以下解决方案为我工作:

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

The above solution satisfies my requirements:

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

  1. All my schema files are on local file system and not hosted by some url
  2. 我的所有架构文件都在本地文件系统上,而不是由某些URL托管

  3. Path specified is relative to the directory where I run mvn goals.
  4. 指定的路径是相对于我运行mvn目标的目录。

#2


0  

This is how I have done it:

我就是这样做的:

Given the following file structure from the root of your application:

从应用程序的根目录提供以下文件结构:

/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" }
        }
    }
}

Have validated with JsonValidator.java

已经通过JsonValidator.java验证

#1


6  

Following solution worked for me:

以下解决方案为我工作:

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

The above solution satisfies my requirements:

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

  1. All my schema files are on local file system and not hosted by some url
  2. 我的所有架构文件都在本地文件系统上,而不是由某些URL托管

  3. Path specified is relative to the directory where I run mvn goals.
  4. 指定的路径是相对于我运行mvn目标的目录。

#2


0  

This is how I have done it:

我就是这样做的:

Given the following file structure from the root of your application:

从应用程序的根目录提供以下文件结构:

/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" }
        }
    }
}

Have validated with JsonValidator.java

已经通过JsonValidator.java验证