我想使用JSON模式验证器进行验证,而我使用如下代码获得错误gCode却未定义
我尝试像下面的代码
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
所if-then-else
讨论的JSON模式的语句不正确。这是正确的:
{
"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的 JSON Schema中添加了,这里是json-schema.org中的文档:https ://json-schema.org/understanding-json-schema/reference/conditionals.html