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"]
}
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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句