JSON Schema:由于对一个以上模式的验证而导致的错误来自' Oneof'

发布于 2025-01-30 08:56:30 字数 4355 浏览 1 评论 0原文

我有一个JSON模式,如果语句 Oneof ,则通过一系列进行检查。但是,由于语句中的一个属性出现在3 中(请参见下面的错误),因此失败了。我如何强制对属性作为组的施加检查?

使用的 https://www.jsonschemavalidator.net/ 验证模式。

模式:

{
  "$id": "https://json-schema.hyperjump.io/schema2",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "Occupancy": {
      "type": "string",
      "enum": [
        "Owner Occupied",
        "Tenant"
      ]
    },
    "DwellingUse": {
      "type": "string",
      "enum": [
        "Dwelling",
        "Apartment",
        "Other Buildings",
        "Townhouses",
        "Condominiums",
        "Duplex",
        "Triplex"
      ]
    },
    "Form": {
      "type": "string",
      "enum": [
        "HO-2 Broad Form",
        "HO-3 Special Form",
        "HO-5 Comprehensive Form"
      ]
    },
    "DwellingAmount": {
      "type": "integer",
      "maximum": 999999999
    },
    "OtherStructuresAmount": {
      "type": "integer",
      "maximum": 999999999,
      "minimum": 0
    }
  },
  "oneOf": [
    {
      "if": {
        "properties": {
          "Occupancy": {
            "const": "Tenant"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-BT Broad Form",
              "HO-CT All-Risk Form",
              "HO-4 Tenant"
            ]
          },
          "DwellingAmount": {
            "const": 0
          },
          "OtherStructuresAmount": {
            "const": 0
          }
        }
      }
    },
    {
      "if": { //Both properties must be checked and match
        "properties": {
          "Occupancy": {
            "const": "Owner Occupied"
          },
          "DwellingUse": {
            "const": "Condominiums"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-B Con Broad Form",
              "HO-C Con All-Risk Form",
              "HO-6 Condominium"
            ]
          },
          "OtherStructuresAmount": {
            "const": 0
          }
        },
        "required": [
          "DwellingAmount"
        ]
      }
    },
    {
      "if": { //Both properties must be checked and match
        "properties": {
          "Occupancy": {
            "const": "Owner Occupied"
          },
          "DwellingUse": {
            "const": "Apartment"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-4 Contents Broad Form"
            ]
          },
          "DwellingAmount": {
            "const": 0
          },
          "OtherStructuresAmount": {
            "const": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "Occupancy": {
            "const": "Owner Occupied"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-A Limited Form",
              "HO-B Broad Form",
              "HO-C All-Risk Form",
              "HO-3 Broad Form",
              "HO-8 Modified Form"
            ]
          }
        },
        "required": [
          "DwellingAmount",
          "OtherStructuresAmount"
        ]
      }
    }
  ],
  "additionalProperties": false
}

验证输入:

{
  "Occupancy": "Owner Occupied",
  "DwellingUse": "Condominiums",
  "Form": "HO-B Con Broad Form",
  "DwellingAmount": 5,
  "OtherStructuresAmount": 0
}

错误:

Found 4 error(s)
Message:
    Value "HO-B Con Broad Form" is not defined in enum.
Schema path:
    https://json-schema.hyperjump.io/schema2#/properties/Form/enum

Message:
    JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1, 2.
Schema path:
    https://json-schema.hyperjump.io/schema2#/oneOf

Message:
    JSON does not match schema from 'then'.
Schema path:
    https://json-schema.hyperjump.io/schema2#/oneOf/3/then/then

Message:
    Value "HO-B Con Broad Form" is not defined in enum.
Schema path:
    https://json-schema.hyperjump.io/schema2#/oneOf/3/then/properties/Form/enum

I have a JSON schema that is checked by series of if statements in oneOf. However, it fails due to the fact that one of the property appears in 3 if statements(see errors below). How do I force checking of properties as groups ?

Used https://www.jsonschemavalidator.net/ to validate schema.

Schema:

{
  "$id": "https://json-schema.hyperjump.io/schema2",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "Occupancy": {
      "type": "string",
      "enum": [
        "Owner Occupied",
        "Tenant"
      ]
    },
    "DwellingUse": {
      "type": "string",
      "enum": [
        "Dwelling",
        "Apartment",
        "Other Buildings",
        "Townhouses",
        "Condominiums",
        "Duplex",
        "Triplex"
      ]
    },
    "Form": {
      "type": "string",
      "enum": [
        "HO-2 Broad Form",
        "HO-3 Special Form",
        "HO-5 Comprehensive Form"
      ]
    },
    "DwellingAmount": {
      "type": "integer",
      "maximum": 999999999
    },
    "OtherStructuresAmount": {
      "type": "integer",
      "maximum": 999999999,
      "minimum": 0
    }
  },
  "oneOf": [
    {
      "if": {
        "properties": {
          "Occupancy": {
            "const": "Tenant"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-BT Broad Form",
              "HO-CT All-Risk Form",
              "HO-4 Tenant"
            ]
          },
          "DwellingAmount": {
            "const": 0
          },
          "OtherStructuresAmount": {
            "const": 0
          }
        }
      }
    },
    {
      "if": { //Both properties must be checked and match
        "properties": {
          "Occupancy": {
            "const": "Owner Occupied"
          },
          "DwellingUse": {
            "const": "Condominiums"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-B Con Broad Form",
              "HO-C Con All-Risk Form",
              "HO-6 Condominium"
            ]
          },
          "OtherStructuresAmount": {
            "const": 0
          }
        },
        "required": [
          "DwellingAmount"
        ]
      }
    },
    {
      "if": { //Both properties must be checked and match
        "properties": {
          "Occupancy": {
            "const": "Owner Occupied"
          },
          "DwellingUse": {
            "const": "Apartment"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-4 Contents Broad Form"
            ]
          },
          "DwellingAmount": {
            "const": 0
          },
          "OtherStructuresAmount": {
            "const": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "Occupancy": {
            "const": "Owner Occupied"
          }
        }
      },
      "then": {
        "properties": {
          "Form": {
            "enum": [
              "HO-A Limited Form",
              "HO-B Broad Form",
              "HO-C All-Risk Form",
              "HO-3 Broad Form",
              "HO-8 Modified Form"
            ]
          }
        },
        "required": [
          "DwellingAmount",
          "OtherStructuresAmount"
        ]
      }
    }
  ],
  "additionalProperties": false
}

Input for validation:

{
  "Occupancy": "Owner Occupied",
  "DwellingUse": "Condominiums",
  "Form": "HO-B Con Broad Form",
  "DwellingAmount": 5,
  "OtherStructuresAmount": 0
}

Error:

Found 4 error(s)
Message:
    Value "HO-B Con Broad Form" is not defined in enum.
Schema path:
    https://json-schema.hyperjump.io/schema2#/properties/Form/enum

Message:
    JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1, 2.
Schema path:
    https://json-schema.hyperjump.io/schema2#/oneOf

Message:
    JSON does not match schema from 'then'.
Schema path:
    https://json-schema.hyperjump.io/schema2#/oneOf/3/then/then

Message:
    Value "HO-B Con Broad Form" is not defined in enum.
Schema path:
    https://json-schema.hyperjump.io/schema2#/oneOf/3/then/properties/Form/enum

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-02-06 08:56:30

很难说出您要寻找的东西。您是否期望您在这里提供的数据应该通过验证?它不会,因为您的“形式”值不在枚举中(根据第一个错误消息)。

还要注意,如果条款根本不存在子句,则将作为true评估 - 因此,为了确保它确实存在,您需要添加“必需“:[<属性名称>]

另外,如果如果子句为false,则else子句将评估。如果没有一个,则结果是正确的 - 因此,如果您有多个条件,如果为false,则您将拥有多个else条款运行,并且如果它们都是正确的,那么您将违反Oneof限制。

有多种解决此问题的方法:要么使用else:false在每个束缚中,要么将Oneof更改为allof

It's hard to tell what you're looking for. Are you expecting that the data you provided here should pass validation? It won't, because your "Form" value isn't in the enum (as per the first error message).

Also be aware that an if clause will evaluate as true if the given property doesn't exist in your data at all -- so to ensure that it does exist, you need to add "required": [<property name>].

Also, if the if clause is false, then the else clause will evaluate. And if there isn't one, then the result is true -- so if you have multiple conditions where the if is false, you will have multiple else clauses running, and if they are all true, then you will violate your oneOf restriction.

There are multiple ways of solving this: either use else: false with every condtional, or change the oneOf to an allOf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文