Anil Arya
Anil Arya

Reputation: 3110

Jsonschema validation fails

I have used jsonschme validator to validate my json ouput against Json files.

from jsonschema import validate    #https://pypi.python.org/pypi/jsonschema

def assertDataMatchesSchema(self, data, schema_file_name): 
        with open(os.path.join("mha/resource_jsonschema", schema_file_name)) as schema_file:
            validate(data, json.load(schema_file))

Here is my jsonschemas:

 { "code": {"type":["string","null"]},
       "codingMethod": {"type":["string","null"]}, 
       "priority":{"type":["string","null"]},
       "status":{"type":["string","null"]} ,
       "description" : {"type" : "string"} 


     }

Terminal output :

SchemaError: {u'type': u'string'} is not of type u'string'

Failed validating u'type' in schema[u'properties'][u'description']:
    {u'type': u'string'}

On instance[u'description']:
    {u'type': u'string'}

Problem: If I remove description field from above file or changed to some other name , Its working but I need description field(required nne) there. Any solution to solve thi issue??

Same problem if I use "type" field there.

Upvotes: 1

Views: 2717

Answers (1)

Anurag
Anurag

Reputation: 1013

description is key which is using by json-schema. So your schema should like:-

schema = {
    "type": "object",
    "properties": {
             "code": {"type":["string","null"]},
       "codingMethod": {"type":["string","null"]}, 
       "priority":{"type":["string","null"]},
       "status":{"type":["string","null"]} ,
       "description" : {"type" : "string"} 
    }
}

data = {"description" : "nowtry"}
validate(data, schema)

It is working for me..

You can see here how your schema should, http://www.w3resource.com/JSON/JSON-Schema.php

Upvotes: 4

Related Questions