2016-04-29 74 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