JSON Schema:由于对一个以上模式的验证而导致的错误来自' Oneof'
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难说出您要寻找的东西。您是否期望您在这里提供的数据应该通过验证?它不会,因为您的“形式”值不在枚举中(根据第一个错误消息)。
还要注意,如果条款根本不存在子句,则将作为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 theelse
clause will evaluate. And if there isn't one, then the result is true -- so if you have multiple conditions where theif
is false, you will have multipleelse
clauses running, and if they are all true, then you will violate youroneOf
restriction.There are multiple ways of solving this: either use
else: false
with every condtional, or change theoneOf
to anallOf
.