基于属性的JSON模式验证 [英] JSON Schema Validation based on a property

查看:76
本文介绍了基于属性的JSON模式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试正确设置JSON模式.我有一个boolean属性,必须根据该属性确定所需的属性.以下是我的示例JSON,我想在没有item3的情况下使验证失败.

I have been trying to get my JSON schema right. I have a boolean property based on which I have to determine the required properties. Below is my sample JSON which I want to fail the validation with item3 not present.

{
  "item1": true,
  "item2": "ABC"
}

这是我要验证通过的JSON

This is the JSON which I want the validation to pass

{
  "item1": true,
  "item2": "ABC",
  "item3": {
    "subItem1": "ABC",
    "subItem2": "BAC"
  }
}

类似地,如果item1false,则上述两个JSON的验证都应通过.

Similarly, if the item1 is false, then the validation should pass for both the above JSON's.

我的JSON模式如下.

My JSON schema for the same is as below.

{
    "definitions": {},
    "type": "object",
    "title": "The Root Schema",
    "properties": {
        "item1": {
            "$id": "#/properties/item1",
            "type": "boolean",
            "title": "The Item1 Schema",
            "default": false,
            "examples": [
                true
            ]
        },
        "item2": {
            "$id": "#/properties/item2",
            "type": "string",
            "title": "The Item2 Schema",
            "default": "",
            "examples": [
                "ABC"
            ],
            "pattern": "^(.*)$"
        },
        "item3": {
            "$id": "#/properties/item3",
            "type": "object",
            "title": "The Item3 Schema",
            "required": [
                "subItem1",
                "subItem2"
            ],
            "properties": {
                "subItem1": {
                    "$id": "#/properties/item3/properties/subItem1",
                    "type": "string",
                    "title": "The Subitem1 Schema",
                    "default": "",
                    "examples": [
                        "AAA"
                    ],
                    "pattern": "^(.*)$"
                },
                "subItem2": {
                    "$id": "#/properties/item3/properties/subItem2",
                    "type": "string",
                    "title": "The Subitem2 Schema",
                    "default": "",
                    "examples": [
                        "BAC"
                    ],
                    "pattern": "^(.*)$"
                }
            }
        }
    },
    "required": ["item1"],
    "allOf": [{
        "if": {
            "properties": {
                "item1": {
                    "enum": [
                        true
                    ]
                }
            }
        },
        "then": {
            "required": [
                "item2",
                "item3"
            ]
        },
        "else": {
            "required": [
                "item2"
            ]
        }
    }]
}

我的验证总是失败.

如果item1为true,则应为subItem2. 如果item1为false,则不需要item3,但仍应进行验证(如果包含).

If item1 is true, subItem2 should be required. If item1 is false, then item3 is not required, but should still validate if included.

推荐答案

您的if/then/else块在验证方面可以正常工作.

Your if/then/else block works correctly in terms of validation.

您提供的希望传递的示例JSON失败,因为您要求item3的属性必须为subItem1subItem2,但没有.

The example JSON you provided that you expect to pass, fails, because you have required that item3 has a property of subItem1 and subItem2, but it does not.

现在,您已经更新了示例JSON,该示例应传递给包含subItem1subItem2的正确的item3,验证将通过您提供的模式传递.

Now you've updated your example JSON that should pass to correct item3 containing subItem1 and subItem2, the validation passes with the schema you've provided.

此外,如果我理解正确,则需要:

Additionally, you want, If I understand correctly:

如果item1为true,则应该要求subItem2.如果item1为假,则 item3不是必需的,但如果包含的话,仍应进行验证.

If item1 is true, subItem2 should be required. If item1 is false, then item3 is not required, but should still validate if included.

将需要subItem3的架构从item3移动到then子句.这样,只有在您的if模式成功验证(item1true)

Move the schema that makes subItem3 required from item3 to your then clause. This will make it so subItem3 is only "required" if your if schema validates successfully (item1 is true)

{
  "definitions": {},
  "type": "object",
  "title": "The Root Schema",
  "properties": {
    "item1": {
      "$id": "#/properties/item1",
      "type": "boolean",
      "title": "The Item1 Schema",
      "default": false,
      "examples": [
        true
      ]
    },
    "item2": {
      "$id": "#/properties/item2",
      "type": "string",
      "title": "The Item2 Schema",
      "default": "",
      "examples": [
        "ABC"
      ],
      "pattern": "^(.*)$"
    },
    "item3": {
      "$id": "#/properties/item3",
      "type": "object",
      "title": "The Item3 Schema",
      "required": [
        "subItem1"
      ],
      "properties": {
        "subItem1": {
          "$id": "#/properties/item3/properties/subItem1",
          "type": "string",
          "title": "The Subitem1 Schema",
          "default": "",
          "examples": [
            "AAA"
          ],
          "pattern": "^(.*)$"
        },
        "subItem2": {
          "$id": "#/properties/item3/properties/subItem2",
          "type": "string",
          "title": "The Subitem2 Schema",
          "default": "",
          "examples": [
            "BAC"
          ],
          "pattern": "^(.*)$"
        }
      }
    }
  },
  "required": [
    "item1"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "item1": {
            "enum": [
              true
            ]
          }
        }
      },
      "then": {
        "required": [
          "item2",
          "item3"
        ],
        "properties": {
          "item3": {
            "required": [
              "subItem2"
            ]
          }
        }
      },
      "else": {
        "required": [
          "item2"
        ]
      }
    }
  ]
}

这篇关于基于属性的JSON模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆