我是JsonSchema的新手,我使用Python来验证后端的响应。现在我遇到了一个错误,我用完了线索。验证函数正在查找"id"
,其中没有定义(参见倒数第二行)。
Traceback (most recent call last):
File "./client.py", line 44, in <module>
j.validate(json.loads(received))
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 129, in validate
for error in self.iter_errors(*args, **kwargs):
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 105, in iter_errors
for error in errors:
File "/usr/local/lib/python2.7/site-packages/jsonschema/_validators.py", line 341, in oneOf_draft4
errs = list(validator.descend(instance, subschema, schema_path=index))
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 121, in descend
for error in self.iter_errors(instance, schema):
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 105, in iter_errors
for error in errors:
File "/usr/local/lib/python2.7/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
schema_path=property,
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 121, in descend
for error in self.iter_errors(instance, schema):
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 105, in iter_errors
for error in errors:
File "/usr/local/lib/python2.7/site-packages/jsonschema/_validators.py", line 304, in properties_draft4
schema_path=property,
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 121, in descend
for error in self.iter_errors(instance, schema):
File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 89, in iter_errors
scope = _schema.get(u"id")
AttributeError: 'unicode' object has no attribute 'get'
我的架构如下所示:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Responses",
"id": "gui_schema.json",
"type": "object",
"properties": {
"uuid": {
"description": "tbd",
"type": "integer"
},
"msgType": {
"description": "Type of message/request",
"type": "string",
"enum": ["global", "config", "command", "status"]
},
"deviceType": {
"description": "tbd",
"type": "integer"
},
"resource": {
"description": "system resource that is requested",
"type": "string"
}
},
"required": ["msgType", "resource"],
"oneOf":
[
{
"type": "object",
"properties": {
"msgType": {
"type": "string",
"enum": ["global"]
},
"resource": {
"type": "string",
"enum": [
"groupParameterSet",
"setpointSources",
"sourceModes",
"operationModes",
"controllerParameter",
"groupLimits",
"groupTrips"
]
}
}
},
{
"type": "object",
"properties": {
"msgType": {
"type": "string",
"enum": ["global"]
},
"resource": {
"type": "string",
"enum": ["systemTree"]
},
"data": {
"type": "object",
"properties": {
"typeName": "string"
}
}
}
}
],
"_additionalProperties": false
}
如果我将"typeName"
替换为"id"
。错误消失了,验证没有断言,一切都很好。我不知道为什么。
以下是我正在验证的数据,其中没有"id"
个键。
{
"componentId": 0,
"csuId": 0,
"data": {
"children": [
{
"children": [
{
"children": [
{
"children": [
],
"childrenCount": 0,
"componentId": 5,
"deviceId": 1,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 6,
"deviceId": 2,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 7,
"deviceId": 3,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 8,
"deviceId": 4,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 9,
"deviceId": 1,
"typeId": 6,
"typeName": "Measurment"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 10,
"deviceId": 2,
"typeId": 6,
"typeName": "Measurment"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 18,
"deviceId": 3,
"typeId": 6,
"typeName": "Measurment"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 19,
"deviceId": 4,
"typeId": 6,
"typeName": "Measurment"
}
],
"childrenCount": 8,
"componentId": 4,
"deviceId": 1,
"typeId": 4,
"typeName": "Group"
},
{
"children": [
{
"children": [
],
"childrenCount": 0,
"componentId": 12,
"deviceId": 1,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 13,
"deviceId": 2,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 14,
"deviceId": 3,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 15,
"deviceId": 4,
"typeId": 3,
"typeName": "Amplifier"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 16,
"deviceId": 1,
"typeId": 6,
"typeName": "Measurment"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 17,
"deviceId": 2,
"typeId": 6,
"typeName": "Measurment"
}
],
"childrenCount": 6,
"componentId": 11,
"deviceId": 2,
"typeId": 4,
"typeName": "Group"
},
{
"children": [
],
"childrenCount": 0,
"componentId": 3,
"deviceId": 1,
"typeId": 5,
"typeName": "PLC"
}
],
"childrenCount": 3,
"componentId": 2,
"deviceId": 1,
"typeId": 2,
"typeName": "Cabinet"
}
],
"childrenCount": 1,
"componentId": 1,
"deviceId": 1,
"typeId": 1,
"typeName": "System"
},
"deviceType": 1,
"groupId": 0,
"msgId": 1,
"msgType": "global",
"resource": "systemTree"
}
python代码很简单:
f=open("gui_schema.json",'r')
schema = json.loads(f.read())
j = jsonschema.Draft4Validator(schema)
...
j.validate(json.loads(received))