jsonschema ищет неопределенный ключ

Я новичок в 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))

0 ответов

Другие вопросы по тегам