我正在尝试构建一个架构,其中包含我想要强制架构的项目列表。
基本上这里是我要针对架构验证的数据:
data = {
"VIN" : "123",
"timestamp" : "xxxx",
"model" : "jeep",
"inspections": [
{ "door_badge" :
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
},
},
{
"rear_badge" :
{
"expected" : "yes",
"image" : "/image/path/here",
"state": 1
}
}
],
}
我的模式映射就是这样,但在尝试验证时似乎出现错误:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type": "string" },
"found": { "type" : "string"},
"state" : { "enum" : [0,1] },
"image" : { "type" : "string"}
},
"required": ["state", "image"]
},
"inspections" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/inspection"
},
"required" : ["items"]
},
},
"type" : "object",
"properties" : {
"VIN" : { "type" : "string" },
"timestamp" : { "type" : "string"},
"model" : { "type" : "string"},
"inspections" : { "$ref" : "#/definitions/inspections"}
},
"required": ["VIN", "timestamp", "model"]
}
我基本上想要检查列表中的子列表,但也要根据该类型的项目进行验证。
解决方案:感谢 jruizaranguren 的帮助,解决方案是:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type": "string" },
"found": { "type" : "string"},
"state" : { "enum" : [0,1] },
"image" : { "type" : "string"}
},
"required": ["state", "image", "expected"]
},
},
"type" : "object",
"properties" : {
"VIN" : { "type" : "string" },
"timestamp" : { "type" : "string"},
"model" : { "type" : "string"},
"inspections" : {
"type" : "array",
"items" : {
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
}
},
"required": ["VIN", "timestamp", "model", "inspections"]
}