【问题标题】:How can I validate the dependency of existance of value for one property value based on the existance of another property value in JSON schema如何根据 JSON 模式中另一个属性值的存在来验证一个属性值是否存在值的依赖关系
【发布时间】:2021-08-08 16:36:56
【问题描述】:

我有一个 JSON 模式:

{
  "type": "object",
  "properties": {
"name": { "type": ["string", "null"] },
"credit_card": { 
     "type": ["string", "null"]
},
"billing_address": {
     "type": ["string", "null"]
}
},

"dependencies": [{
"credit_card": ["billing_address"]
}]

}

如果提供了 credit_card 值,我希望显示 billing_address 值。但是,由于我已将 billing_address 的类型指定为 null,因此即使存在 credit_card 值并因此无法验证,它也接受 null 值。有人可以建议这样做的正确方法。 提前致谢。

【问题讨论】:

    标签: java json schema json-schema-validator


    【解决方案1】:

    您的dependencies 应定义为对象{} 而不是数组[]。您只需要删除外部方括号:

    "dependencies": {
      "credit_card": ["billing_address"]
    }
    

    总的来说,这给出了以下架构:

    {
        "type": "object",
        "properties": {
            "name": {
                "type": ["string", "null"]
            },
            "credit_card": {
                "type": ["string", "null"]
            },
            "billing_address": {
                "type": ["string", "null"]
            }
        },
        "dependencies": {
            "credit_card": ["billing_address"]
        }
    }
    

    使用上述模式,以下 JSON 是有效的:

    {
      "name": "Abel",
      "credit_card": "1234...",
      "billing_address": "some address here..."
    }
    

    但是下面的 JSON 无效

    {
      "name": "Abel",
      "credit_card": "1234"
    }
    

    您可以使用在线验证器(例如 this one)来测试这些。


    您可能还需要考虑删除您在架构中使用的null 值。例如,通过使用:

    {
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
            },
            "credit_card": {
                "type": "string",
            },
            "billing_address": {
                "type": "string",
            }
        },
        "dependencies": {
            "credit_card": ["billing_address"]
        }
    }
    

    使用此修改后的架构,您现在还会收到 JSON 验证错误,如下所示:

    {
      "name": "Abel",
      "credit_card": null,
      "billing_address": "some address here..."
    }
    

    更新 - 两个字段都存在但为空:

    如果credit_cardbilling_address 都为空,则可以使用条件验证(添加到下面架构的末尾)来处理这种情况:

    {
        "type": "object",
        "properties": {
            "name": {
                "type": ["string", "null"]
            },
            "credit_card": {
                "type": ["string", "null"]
            },
            "billing_address": {
                "type": ["string", "null"]
            }
        },
        "dependencies": {
            "credit_card": ["billing_address"]
        },
        "if": {
          "properties": { "credit_card": { "const": null } }
        },
        "then": {
          "properties": { "billing_address": { "const": null } }
        }
    }
    

    现在,以下内容也将有效:

    {
      "name": "Abel",
      "credit_card": null,
      "billing_address": null
    }
    

    一个警告说明:这使用了 JSON Schema 规范的一个相对较新的特性。我上面提到的在线验证器支持它 - 但我不知道你可能使用的任何验证器是否支持它。

    【讨论】:

    • 感谢您的回复。但是当两个字段都为空时,这样做对我没有帮助。当两个字段都为空时,我希望我的架构被认为是有效的。 @andrewjames
    • 已更新以处理两个字段都存在但为空的情况。
    • 我认为问题在于对 JSON 版本的支持。我正在使用mule 4,并且在if then else 中添加依赖项。我认为 if then else 是 JSON 7 功能,而 mule 不支持该功能。 @andrewjames 您是否知道在 JSON 04 中可以用作 if then else 的替代方法?
    • 我已经使用 oneOf 解决了这个问题,它在 JSON 草案 04 中支持作为 if else then 的替代方案。它成功地工作。 :)
    • 很高兴您有一个适合您的解决方案。欢迎您来到answer your own question,这样其他人就可以确切地看到您是如何使用您的方法解决问题的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多