2016-04-29 68 views
2

我已经写了一个小块os的json模式,但我得到一个验证错误使用python jsonschema。验证'type'json schema失败

这里是我的架构:

{ 


"$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的属性吗?

+0

我以前的评论是不正确的。我尝试加载你的JSON在我的外壳,它工作正常。 'check_schema'是期待json对象吗? –

+0

'check_schema'正在寻找'dic' – visc

+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