【问题标题】:Merge properties of one Json schema in another Json schema将一个 Json 模式的属性合并到另一个 Json 模式中
【发布时间】:2016-05-11 06:09:21
【问题描述】:

我有一个架构,我必须将其属性合并到另一个架构中。假设以下是必须包含其属性的架构:

{"$schema": "http://json-schema.org/schema#",
"title": "native",
"type": "object",
"required": ["executable"],
"properties":
{
  "job_name":
  {
    "type":"string",
    "dname": "job name"
  },
  "executable":
  {
    "type":"string",
    "dname":"server",
    "description": "server name"
  }
}}

包含上述架构的架构为:

{"$schema": "http://json-schema.org/schema#",
"title": "application",
"type": "object",
"required": ["app_name"],
"properties":
{
  "app_name":
  {
    "type":"string",
    "dname": "app name"
  }
}}

我想将第一个模式的属性合并到第二个模式中,使第二个模式看起来像:

{"$schema": "http://json-schema.org/schema#",
"title": "native",
"type": "object",
"required": ["executable","app_name"],
"properties":
{
  "job_name":
  {
    "type":"string",
    "dname": "job name"
  },
  "executable":
  {
    "type":"string",
    "dname":"server",
    "description": "server name"
  }
  "app_name":
  {
    "type":"string",
    "dname": "app name"
  }
}}

此外,“必填”字段被添加到第二个架构中。 有没有可能的方法?

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    您不能合并模式,但可以使用allOf 要求文档针对两种模式进行验证。它与合并不同,但它适用于您提供的案例。

    {
      "allOf": [
        { ... schema1 ... },
        { ... schema2 ... }
      ]
    }
    

    【讨论】:

      【解决方案2】:

      我可以使用 'extends' 关键字在另一个模式中扩展一个模式,如下所示:

      • 在定义的同一个文件中定义第一个模式(我希望它也可以是另一个json模式文件)并在另一个模式中扩展它。

        "definitions" : {
         "argumentBaseType":{
         "required": ["executable"],
         "properties":
         {
          "job_name":
          {
           "type":"string",
           "dname": "job name"
          },
         "executable":
         {
          "type":"string",
          "dname":"server",
          "description": "server name"
         }
        }
        }
        "properties":
        {
        {
         Argument1 : {
          "extends" : {
            "$ref" : "#/definitions/argumentBaseType"
          },
          "app_name":
          {
           "type":"string",
           "dname": "app name"
          }
         }}
        

      【讨论】:

        猜你喜欢
        • 2022-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多