很抱歉,如果这个问题令人困惑。我正试图了解jsonschema验证。我试图解决的问题是将属性设置为可选。该属性属于一个必需的对象,而该对象又是复杂类型的属性。
e.g。
{ "prop1" : {
"field1" : {"type" : "string" },
"field2" : {"type" : "string" }
},
"prop2" : { "type" : "string" }
}
如果我想声明prop1是必需的但prop1.field1是可选的,我该如何用jsonschema做到这一点?
谢谢, 罗宾
答案 0 :(得分:3)
您需要指定" required"与"属性相同的级别"。
{
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"prop1": {
"type": "object",
"properties": {
"field1": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"field2": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
}
},
"prop2": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
},
"required" : [ "prop1" ]
}
此架构是使用此在线工具生成的(加上一些编辑):