如何在 JSON Schema Validator 中使用 if else 条件
How to use if else condition in JSON Schema Validator
我想使用 JSON 模式验证器进行验证,而我使用如下代码时出现错误 gCode is not defined
我试过如下代码
properties: {
oemId: {
'type': ['integer'],
'minLength': 1,
'required': true
},
gCode:{
'type': ['string'],
'minLength': 1
},
problemCategoryId: {
'type': ['integer'],
'minLength': 1
}
},
if :{
properties:{
oemId: 1
}
},
then:{
required:[gCode]
},
else:{
required: [problemCategoryId]
}
我预计当 oemId=1 时需要 gCode=true 否则需要 problemCategoryId true
有问题的 JSON 模式的 if-then-else
语句不正确。这是正确的:
{
"type": "object",
"properties": {
oemId: {
'type': ['integer'],
'minLength': 1,
'required': true
},
gCode:{
'type': ['string'],
'minLength': 1
},
problemCategoryId: {
'type': ['integer'],
'minLength': 1
}
},
"if": {
"properties": {
"oemId": { "const": 1 }
},
"required": ["oemId"]
},
"then": { "required": ["gCode"] },
"else": { "required": ["problemCategoryId"] }
}
请注意此 if-then-else
语法刚刚添加到 draft-07, and here its document in json-schema.org: https://json-schema.org/understanding-json-schema/reference/conditionals.html
中的 JSON 架构
我想使用 JSON 模式验证器进行验证,而我使用如下代码时出现错误 gCode is not defined
我试过如下代码
properties: {
oemId: {
'type': ['integer'],
'minLength': 1,
'required': true
},
gCode:{
'type': ['string'],
'minLength': 1
},
problemCategoryId: {
'type': ['integer'],
'minLength': 1
}
},
if :{
properties:{
oemId: 1
}
},
then:{
required:[gCode]
},
else:{
required: [problemCategoryId]
}
我预计当 oemId=1 时需要 gCode=true 否则需要 problemCategoryId true
有问题的 JSON 模式的 if-then-else
语句不正确。这是正确的:
{
"type": "object",
"properties": {
oemId: {
'type': ['integer'],
'minLength': 1,
'required': true
},
gCode:{
'type': ['string'],
'minLength': 1
},
problemCategoryId: {
'type': ['integer'],
'minLength': 1
}
},
"if": {
"properties": {
"oemId": { "const": 1 }
},
"required": ["oemId"]
},
"then": { "required": ["gCode"] },
"else": { "required": ["problemCategoryId"] }
}
请注意此 if-then-else
语法刚刚添加到 draft-07, and here its document in json-schema.org: https://json-schema.org/understanding-json-schema/reference/conditionals.html