验证失败'类型' json架构

时间:2016-04-29 15:41:23

标签: python json

我已经编写了一个小块的os json架构,但是我使用python jsonschema得到了验证错误。

这是我的架构:

{


"$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "output": {
      "type": "object",
      "properties": {
        "Type": {
          "type": "object",
          "properties": {
            "Type": {
              "type": "string"
            },
            "Value": {
              "type": "string"
            },
            "Default": {
              "type": "string"
            },
            "Description": {
              "type": "string"
            },
            "Options": {
              "type": "array"
            }
          },
          "required": [
            "Type",
            "Value",
            "Default",
            "Description",
            "Options"
          ]
        },
        "Inverted": {
          "type": "object",
          "properties": {
            "Type": {
              "type": "string"
            },
            "Value": {
              "type": "bool"
            },
            "Default": {
              "type": "bool"
            },
            "Description": {
              "type": "string"
            }
          },
          "required": [
            "Type",
            "Value",
            "Default",
            "Description"
          ]
        },
        "Pulse Width": {
          "type": "object",
          "properties": {
            "Type": {
              "type": "string"
            },
            "Value": {
              "type": "number"
            },
            "Default": {
              "type": "number"
            },
            "Description": {
              "type": "string"
            }
          },
          "required": [
            "Type",
            "Value",
            "Default",
            "Description"
          ]
        }
      },
      "required": [
        "Type",
        "Inverted",
        "Pulse Width"
      ]
    }
  }
}

以下是我收到的错误:

Failed validating u'type' in schema

我试图通过以下方式验证我的架构:

schema = ""
with open(jsonSchemaFilePath, 'r') as schema_file:
    schema = schema_file.read()

try:
    Draft4Validator.check_schema(schema)
except SchemaError as schemaError:
    print schemaError

我在编写的架构上做错了什么?我不允许拥有名为Type?的属性吗?

1 个答案:

答案 0 :(得分:1)

我的问题是Draft4Validator.check_schema使dic不是字符串,也不是json对象。

这是我的解决方案:

schema = {}
with open(jsonSchemaFilePath, 'r') as schema_file:
    schema = json.loads(schema_file.read())

try:
    Draft4Validator.check_schema(schema)
except SchemaError as schemaError:
    print schemaError