【问题标题】:if then else condition in json schemaif then else条件在json模式中
【发布时间】:2023-03-22 00:39:02
【问题描述】:
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"required": [
    "FileID",
    "MString",
    "desc",
    "elements"
],
"properties": {
    "FileID": {
        "type": "string"
    },
    "MString": {
        "type": "string"
    },
    "desc": {
        "type": "string"
    },
    "elements": {
        "type": "array",
        "items": {
            "type": "object",
            "required": [
                "Mytype",
                "filename",
                "compression"
            ],
            "properties": {
                "Mytype": {
                    "type": "string"
                },
                "filename": {
                    "type": "string"
                },
                "compression": {
                    "type": "string"
                }
            }
        }
    }
}

}

我想在这个模式中添加一个条件(如果 Mytype == "abc")那么 "filename" 只能是 "tutor.txt"。请有人帮我写这个在模式中添加这个条件。我尝试了很多方法,但总是给出一些错误。提前致谢。

【问题讨论】:

    标签: javascript python json.net


    【解决方案1】:

    有不同的方法可以实现这种行为。一种是使用条件就地应用程序,它从 JSON Schema Draft 7 开始可用。语法如下(为了清楚起见,我将提取涉及的子模式):

    {
      "type": "object",
      "required": [
        "Mytype",
        "filename",
        "compression"
      ],
      "properties": {
        "Mytype": {
          "type": "string"
        },
        "filename": {
          "type": "string"
        },
        "compression": {
          "type": "string"
        }
      },
      "if": {
        "properties": {"Mytype": {"const": "abc"}}
      },
      "then": {
        "properties": {"filename": {"const": "tutor.txt"}}
      }
    }
    

    以下 JSON 文档对此 JSON Schema 有效:

    {
      "Mytype": "abc",
      "filename": "tutor.txt",
      "compression": "foo"
    }
    

    这将是无效

    {
      "Mytype": "abc",
      "filename": "tutor",
      "compression": "foo"
    }
    

    对于更复杂的条件,JSON Schema 提供了一个"else" 关键字。要了解更多信息,我建议您查看this educative reference,在有条件地应用子模式部分下。

    在这种条件方法之上,您可以使用布尔逻辑应用程序而不是上面演示的条件应用程序来表达相同的语法。就个人而言,虽然通常更冗长,但我更喜欢这种解决方案,因为它代表了一个纯粹的声明性解决方案,而不是那种“程序性”if-then-else 概念,毕竟保持更清晰。

    示例如下:

    {
      "type": "object",
      "required": [
        "Mytype",
        "filename",
        "compression"
      ],
      "oneOf": [
        {
          "properties": {
            "Mytype": {
              "const": "abc"
            },
            "filename": {
              "const": "tutor.txt"
            },
            "compression": {
              "type": "string"
            }
          }
        },
        {
          "properties": {
            "Mytype": {
              "not": {
                "const": "abc"
              }
            },
            "filename": {
              "type": "string"
            },
            "compression": {
              "type": "string"
            }
          }
        }
      ]
    }
    

    说明:JSON 文档必须对"oneOf" 中包含的子模式之一有效。第一个表示如果"Mytype""abc",则强制"filename""tutor.txt" 的约束情况。第二个子模式表示"Mytype" 不是not "abc" 的情况,因此"filename" 不受约束,只是任何"string"

    如上所述,布尔逻辑应用程序语法会产生更冗长的模式,它们可能需要一些时间来适应它们,但总的来说,它们是我认为的最佳选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多