JSON模式条件语句 [英] JSON Schema Conditional Statements

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

问题描述

我正在尝试验证我认为是简单的JSON模式作为我的Python应用程序的配置文件的情况,它是标头键/值对的列表,唯一的麻烦是,如果类型"字段设置为"AnyValue",则不需要值键.

I am trying to validate what I thought was a simple JSON schema as a configuration file for my Python application, it's a list of header key/value pairs, the only complication is that if the 'Type' field is set to 'AnyValue' then the value key is not required.

以下是架构:

{
    "definitions":
    {
        'KeyEntry':
        {
             "properties":
             {
                'Type': {"type" : "string"},
                'Key': {"type": "string"}
             },
             "required": ['Type', 'Key'],
            "anyOf":
            [
                {
                    "if":
                    {
                        "properties": {'Type': {"const": 'ExactValue'}}
                    },
                    "then":
                    {
                        "properties":
                        {
                            'Value': {"type": "string"}
                        },
                        "required": ['Type', 'Key', 'Value'],
                        "additionalProperties": false
                    }
                },
                {
                    "if":
                    {
                        "properties": {'Type': {"const": 'AnyValue'}}
                    },
                    "then":
                    {
                        "required": ['Type', 'Key'],
                        "additionalProperties": false
                    }
                }
            ]
        }
    },

    "type": "object",
    "properties":
    {
        'Keys':
        {
            "type": "array",
            "items": {"$ref": "#/definitions/KeyEntry"}
        }
    },
    "required": ['Keys']
}

大多数验证有效,除非我添加了额外的值,即使我在整个模式中都设置了"additionalProperties":false.

Most of the validation works, except if I add extra values, even though I have set "additionalProperties": false throughout the schema.

以下是接受额外值的示例:

Here is an example where extra values are accepted:

{
    "Keys": [

        {
            "Type": "AnyValue",
            "Key": "Version",
            "Y": "Yes",
            "N": "No",
        }
    ]
}

请有人帮忙解释我哪里出了问题以及如何纠正它吗?

Please can someone help explain where I have gone wrong and how I should correct it, please?

推荐答案

additionalProperties draft-07...

带有"additionalProperties"的验证仅适用于实例名称的子值,这些实例名称与"properties"中的任何名称都不匹配,并且与"patternProperties"中的任何正则表达式都不匹配.

Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".

这意味着additionalProperties仅知道出现在properties中或与patternProperties中的正则表达式匹配的关键字.在没有properties的情况下将additionalPropertiesrequired一起使用将创建一个过时的架构(没有任何东西会通过验证).

This means that additionalProperties is only aware of keywords which appear in properties or that match the regex from patternProperties. Using additionalProperties with required without properties is going to create a dud schema (nothing will pass validation).

相反,您可以将关注点分离为您真正想要的...

In stead, you can separate the concerns into what you actually want...

  • 您希望类型,键和值全部为字符串. 之一...
  • 如果类型为AnyValue,则仅需要类型和密钥.
  • 如果类型为ExactValue,则需要类型,键和值.
  • You want type, key, and value, to all be strings. One of...
  • If type is AnyValue, then require type and key only.
  • If type is ExactValue, then require type, key, and value.

这也更容易理解.这里有一个的

This is also easier to understand. Here's a live demo with your data.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "KeyEntry": {
      "properties": {
        "Type": {
          "type": "string"
        },
        "Key": {
          "type": "string"
        },
        "Value": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": [
        "Type",
        "Key"
      ],
      "anyOf": [
        {
          "if": {
            "properties": {
              "Type": {
                "const": "ExactValue"
              }
            }
          },
          "then": {
            "required": [
              "Type",
              "Key",
              "Value"
            ]
          },
          "else": false
        },
        {
          "if": {
            "properties": {
              "Type": {
                "const": "AnyValue"
              }
            }
          },
          "then": {
            "required": [
              "Type",
              "Key"
            ]
          },
          "else": false
        }
      ]
    }
  },
  "type": "object",
  "properties": {
    "Keys": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/KeyEntry"
      }
    }
  },
  "required": [
    "Keys"
  ]
}

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

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