【问题标题】:JSON Schema - if then statement is always evaluated even when "if" isn't trueJSON Schema - if then 语句总是被评估,即使“if”不正确
【发布时间】:2021-10-14 18:10:29
【问题描述】:

我有一个 JSON 模式,在模式中 - 当属性 type 等于 menu 时,属性 default 应该是一个整数或称为“默认”的字符串。问题是,架构会引发警告/错误,即 default 属性应该是整数,即使 type 不是“菜单”也是如此。

(JSON 中有名为“type”和“default”的属性,不要与 JSON 模式类型和默认键混淆)。

Schema 缩减为相关属性:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "array",
    "title": "HubSpot Module",
    "items": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "description": "The type of field, see [field types](https://developers.hubspot.com/en/docs/cms/building-blocks/module-theme-fields#field-types) for documentation on all field types."
        },
        "default": {
          "type": ["string", "integer", "array", "boolean", "null", "number", "object"],
          "description": "Default value for the field.",
          "if": {
            "properties": {
              "type": {
                "const": "menu"
              }
            }
          },
          "then": {
            "anyOf": [
              {
                "type": "integer"
              },
              {
                "type": "string",
                "enum": [
                  "default"
                ]
              }
            ],
            "description": "The menu ID for the menu. The default value of null, defaults to the default menu under navigation.",
            "default": "null"
          },
          "else": {
            "anyOf": [
              {
                "type": ["string", "integer", "array", "boolean", "null", "number", "object"]
              }
            ]
          }
        }
      },
      "required": [
        "name",
        "label",
        "type"
      ]
    }
  }

当满足这些条件时它会起作用,例如

[
  {
    "label": "Primary menu field",
    "name": "primary_menu_field",
    "type": "menu",
    "default": 123
  }
]

[
  {
    "label": "Primary menu field",
    "name": "primary_menu_field",
    "type": "menu",
    "default": "default"
  }
]

这些都不会引发警告。但是以下会引发警告:Incorrect type. Expected "integer".

[
  {
    "name": "boolean_field",
    "label": "Boolean field",
    "required": false,
    "locked": false,
    "type": "boolean",
    "inline_help_text": "",
    "help_text": "",
    "default": false 
  }
]

尽管type 属性没有被命名为“菜单”。将鼠标悬停在“默认”属性 VSCode 上确实给了我对它的定义。

似乎if 语句总是被评估为真 - 只有当属性 type 的值是 menu 时才应该评估它,我有什么遗漏吗?

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    您的if 没有按预期工作,因为它位于错误的位置并且没有受到足够的约束。您在“默认”属性架构中有if,这意味着if 架构将应用于“默认”属性的值。所以,如果“默认”是123,那么if 模式,

    {
      "properties": {
        "type": { "const": "menu" }
      }
    }
    

    将根据123 进行评估。由于123 是一个数字,properties 关键字被忽略,架构将通过验证,then 架构将适用。这就是我所说的if 受到约束的意思。使用if时需要检查是否有缺失数据或数据类型错误。

    {
      "type": "object",
      "properties": {
        "type": { "const": "menu" }
      },
      "required": ["type"]
    }
    

    现在,在针对此架构验证 123 时,您将收到一条错误消息,并希望找出您的 if/then 位于错误的位置。 if 需要应用于对象,您需要将其移至架构中的该级别。这将使您的if 按预期进行评估。不要忘记更新then,因为您移动了架构。我还需要在对象级别而不是属性级别进行定义。

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
    
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": { "type": "string" }
        },
        "required": ["name", "label", "type"],
    
        "if": {
          "type": "object",
          "properties": {
            "type": { "const": "menu" }
          },
          "required": ["type"]
        },
        "then": {
          "properties": {
            "default": {
              "anyOf": [
                { "type": "integer" },
                { "const": "default" }
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您的if/then/elsedefault 属性下,因此它在default 下寻找对象,而不是在您想要的上一层。

      此外,如果属性实际上不存在,if 条件中的properties 检查将始终为真,因此为确保它确实存在,您可以在其旁边添加一个"required": <propertyname>

      (正交地,您的 type 检查实际上并没有做任何事情,因为您将所有有效类型都包含在列表中。您不妨将其省略以指示“这可以是任何类型”。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 1970-01-01
        相关资源
        最近更新 更多