【问题标题】:Add additional properties to json array member schema向 json 数组成员模式添加附加属性
【发布时间】:2022-11-18 05:01:33
【问题描述】:

我正在尝试为一些用作脚本的 json 文件创建 2 个单独的模式。 一个模式应该包含所有可能的命令,而另一个模式应该只包含命令的一个子集。

我的想法是我首先创建最小模式:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "$schema": {},
        "commands": {
            "type": "array",
            "items":
            {
                "title": "One step of the migration",
                "type": "object",
                "minProperties": 1,
                "maxProperties": 1,

                "properties": {
                    "simple_command_1": {"type": "object"},
                    "simple_command_2": {"type": "object"},
                }
            }
        }
    }
}

这将像这样验证 json 文件:

{
    "$schema": "../../migration_schema_v1.json",
    "commands": [
        {"simple_command_1": {}},
        {"simple_command_1": {}},
        {"simple_command_2": {}},
        {"simple_command_2": {}},
    ]

我试图避免使用extend这个词,但是...我希望能够创建第二个模式,其中包含第一个模式具有的每个命令和一个额外的advanced_command_3。所以我创建了这个:

    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$ref": "/migration_schema_v1.json#/",
    "type": "object",
    "properties": {
        "foo": {
            "type": "object",
            "title": "this does work"
        },
        "commands": {
            "items":
            {
                "properties": {
                    "advanced_command_3": {"type": "object"}
                }
            }
        }
    }
}

我可以引用第二个模式,它确实继承了第一个模式的所有内容,但我真的无法添加任何属性(命令)

但是,我能够在根目录中的“命令”旁边添加其他属性。请参见“foo”属性。

在子属性中使用 allOf[]$ref 似乎没有什么区别。

我弄错了吗? 谢谢!

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    我实际上会将其分为三个模式。第一个包含数据的主要定义,包括所有有效命令。

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://example.com/definitions",
      "type": "object",
      "properties": {
        "$schema": {},
        "commands": {
          "type": "array",
          "items":
          {
            "title": "One step of the migration",
            "type": "object",
            "minProperties": 1,
            "maxProperties": 1,
    
            "properties": {
              "simple_command_1": {"type": "object"},
              "simple_command_2": {"type": "object"},
              "advanced_command_3": {"type": "object"}
            }
          }
        }
      }
    }
    

    另外两个是您的简单模式和高级模式。他们的工作方式是定义只有你想要的命令的可能性是必需的然后参考定义模式以获得其余的要求。

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://example.com/simple",
      "$ref": "definitions",
      "properties": {
        "commands": {
          "items": {
            "anyOf": [
              { "required": ["simple_command_1"] },
              { "required": ["simple_command_2"] }
            ]
          }
        }
      }
    }
    
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://example.com/advanced",
      "$ref": "definitions",
      "properties": {
        "commands": {
          "items": {
            "anyOf": [
              { "required": ["simple_command_1"] },
              { "required": ["simple_command_2"] },
              { "required": ["advanced_command_3"] }
            ]
          }
        }
      }
    }
    

    我认为此设置包含最少的重复。


    此时你可能能够摆脱你想用extends做的事情。您必须将简单模式中的命令要求提取到 $defs 关键字中,并从高级模式中引用它们。

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://example.com/simple",
      "$defs": {
        "simpleCommands": {
          "anyOf": [
            { "required": ["simple_command_1"] },
            { "required": ["simple_command_2"] }
          ]
        }
      },
      "$ref": "definitions",
      "type": "object",
      "properties": {
        "commands": {
          "items": { "$ref": "#/$defs/simpleCommands" }
        }
      }
    }
    
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://example.com/advanced",
      "$ref": "definitions",
      "type": "object",
      "properties": {
        "commands": {
          "items": {
            "anyOf": [
              { "$ref" : "simple#/$defs/simpleCommands" },
              { "required": ["advanced_command_3"] }
            ]
          }
        }
      }
    }
    

    我还没有测试过这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      相关资源
      最近更新 更多