问题
I am trying to build a schema which has a list of items that I want to have an enforced schema.
Basically here is the data I want to validate against the schema:
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
}
}
],
}
And my schema mapping as is such but seem to be getting errors when trying to validate:
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"]
}
I basically want sub list within the inspection list but also to validate based on that type of item.
SOLUTION: Thanks to jruizaranguren's help the solution was:
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"]
}
回答1:
The problem you have is that you are constraining each item in the array to be of the form:
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
}
But your objects are of the form:
{ "door_badge" :
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
},
}
One way to achieve this would be to use additionalProperties
in the items
clause:
"items" :
{
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
If you can enforce some rules on these properties keys (for instance, all must end with badge) then you may use patternProperties
clause with a regular expression.
来源:https://stackoverflow.com/questions/35545812/how-do-i-do-a-nested-list-array-of-schema-references-in-json-schema