如何在 json 不是字典的模式中创建模式引用的嵌套列表(数组)
How do I do a nested list (array) of schema references in json schema that isn't a dictionary
所以我有一个类似的问题(参见:),但现在我的结构发生了一些变化,似乎无法验证。
data = {
'VIN': '1234567',
'Vehicle color': blue,
'inspections': [
{'expected': 'MVA',
'found': 0.0,
'inspection': 'Fascia',
'location': 'rear_left',
'state': None},
{'expected': 'MVA',
'found': 0.0,
'inspection': 'Fascia',
'location': 'rear_right',
'state': None},
{'expected': 'UNKNOWN',
'found': 'CPW7',
'inspection': 'liftGateHandle',
'location': 'center_bottom',
'state': True},
{'expected': 'tinted',
'found': 'tinted',
'inspection': 'rearWindowtint',
'location': 'center_top',
'state': True},
],
'model': 'racecar',
'timestamp': '2016-03-03 01:44:00.616000'
}
我在此处使用与之前 link 中列出的相同架构:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type" : "string" },
"found": { "type" : "string"},
"state" : { "type" : "string" },
"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"]
}
我试过使用数组而不是对象来定义,但不幸的是我在尝试验证时遇到以下错误:
ValidationError: 'black' is not of type 'object'
Failed validating 'type' in schema['properties']['inspections']['items']['additionalProperties']:
{'properties': {'expected': {'type': 'string'},
'found': {'type': 'string'},
'image': {'type': 'string'},
'state': {'enum': [0, 1]}},
'required': ['state', 'image', 'expected'],
'type': 'object'}
On instance['inspections'][0]['expected']:
'black'
问题出在对上一个问题同样的误解。在 inspections
的规范中,您有:
"inspections" : {
"type" : "array",
"items" : {
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
}
这意味着inspections
必须是一个数组,它的项目必须是objects
和单个属性。 属性 必须符合 #/definitions/inspection
模式。
根据您当前的架构,inspections
项应该是这样的:
"inspections" : [{
"anyKeyIsValidHere" : {
"expected" : "MVA",
"found" : 0.0,
"inspection" : "Fascia",
"location" : "rear_left",
"state" : 0
}
}
]
因此,在这种情况下,与您之前的问题相反,您的 inspections
项目应该是这样的:
"inspections" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/inspection"
}
}
最后的建议。尝试逐步构建模式,确保正确执行每个所需的约束。这也有助于提出更有针对性的 SO 问题。
所以我有一个类似的问题(参见:
data = {
'VIN': '1234567',
'Vehicle color': blue,
'inspections': [
{'expected': 'MVA',
'found': 0.0,
'inspection': 'Fascia',
'location': 'rear_left',
'state': None},
{'expected': 'MVA',
'found': 0.0,
'inspection': 'Fascia',
'location': 'rear_right',
'state': None},
{'expected': 'UNKNOWN',
'found': 'CPW7',
'inspection': 'liftGateHandle',
'location': 'center_bottom',
'state': True},
{'expected': 'tinted',
'found': 'tinted',
'inspection': 'rearWindowtint',
'location': 'center_top',
'state': True},
],
'model': 'racecar',
'timestamp': '2016-03-03 01:44:00.616000'
}
我在此处使用与之前 link 中列出的相同架构:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type" : "string" },
"found": { "type" : "string"},
"state" : { "type" : "string" },
"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"]
}
我试过使用数组而不是对象来定义,但不幸的是我在尝试验证时遇到以下错误:
ValidationError: 'black' is not of type 'object'
Failed validating 'type' in schema['properties']['inspections']['items']['additionalProperties']:
{'properties': {'expected': {'type': 'string'},
'found': {'type': 'string'},
'image': {'type': 'string'},
'state': {'enum': [0, 1]}},
'required': ['state', 'image', 'expected'],
'type': 'object'}
On instance['inspections'][0]['expected']:
'black'
问题出在对上一个问题同样的误解。在 inspections
的规范中,您有:
"inspections" : {
"type" : "array",
"items" : {
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
}
这意味着inspections
必须是一个数组,它的项目必须是objects
和单个属性。 属性 必须符合 #/definitions/inspection
模式。
根据您当前的架构,inspections
项应该是这样的:
"inspections" : [{
"anyKeyIsValidHere" : {
"expected" : "MVA",
"found" : 0.0,
"inspection" : "Fascia",
"location" : "rear_left",
"state" : 0
}
}
]
因此,在这种情况下,与您之前的问题相反,您的 inspections
项目应该是这样的:
"inspections" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/inspection"
}
}
最后的建议。尝试逐步构建模式,确保正确执行每个所需的约束。这也有助于提出更有针对性的 SO 问题。