【问题标题】:JSON Schema - How can I conditionally refer to the JSON file based on the field valueJSON Schema - 如何根据字段值有条件地引用 JSON 文件
【发布时间】:2018-05-01 03:44:16
【问题描述】:
    {
        "operation": {
                       "type": "string",
                       "enum": ["create","update"]
         },
        "payload": {
    // I have to add json file refs: based on the value of operation(create,update)
        }
    }

如果操作是创建比 ("$ref": "create" ) json 文件。

如果它比 ($ref - "update") json 文件更新

【问题讨论】:

    标签: arrays json schema jsonschema


    【解决方案1】:

    答案取决于您使用的草稿。

    04 草案:

    {
        "oneOf": [
            {
                "operation": {
                    "type": "string",
                    "enum": ["create"]
                },
                "payload": {"$ref": "create"}
            },
            {
                "operation": {
                    "type": "string",
                    "enum": ["update"]
                },
                "payload": {"$ref": "update"}
            }
        ]
    }
    

    draft-06(注意"const" 代替了单元素"enum"s):

    {
        "oneOf": [
            {
                "operation": {
                    "type": "string",
                    "const": "create"
                },
                "payload": {"$ref": "create"}
            },
            {
                "operation": {
                    "type": "string",
                    "const": "update"
                },
                "payload": {"$ref": "update"}
            }
        ]
    }
    

    draft-07(周一发布,因此尚未真正实施)介绍了"if"/"then"/"else",它提供了其他几种方法来做同样的事情。但我认为"oneOf" 在这种情况下非常清楚。

    【讨论】:

      【解决方案2】:

      新版本的 avro 允许 "if"/"then"/"else" 条件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-25
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-19
        • 2020-07-18
        相关资源
        最近更新 更多