Anil Arya
Anil Arya

Reputation: 3110

Schema validation error

I have used a field 'timestamp' is of type 'datetime.datetime' used in models.py in one of the django project,

Here is data :

{"bedId": "5807a14", "description": "", 

"timestamp": "2013-09-16T15:40:16.383133", "encounterId": null, "patientId": null, "type": 

"end_of_shift_note", "triggeredBy": "abc"}

Here is schema :

schema =
 {
    "type":"object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "http://jsonschema.net",
    "required":true,
    "properties":{
        "type": {
            "type":"string",
            "id": "http://jsonschema.net/bedNumber",
            "required":true
        },
        "encounterId": {
            "type":"string",
            "id": "http://jsonschema.net/encounterId",
            "required":true
        },
        "timestamp": { 
            "type" :["null","string","date-time"],
            'required' :false
        },
        "bedId": {
            "type":"string",
            "id": "http://jsonschema.net/cleaningStatus",
            "required":true
        },
        "patientId": {
            "type":["string","null"],
            "id": "http://jsonschema.net/facilityId",
            "required":true
        } ,
        "id": {
            "type":["string","null"],
            "id": "http://jsonschema.net/id",
            "required":false
        },
        "triggeredBy": {
            "type":["string","null"],
            "id": "http://jsonschema.net/lastCleanedTime",
            "required":false
        } 
    }
}

On doing schema validation : https://pypi.python.org/pypi/json-schema-validator

def assertDataMatchesSchema(self, data, schema_file_name): 
    with open(os.path.join("hma/resource_jsonschema", schema_file_name)) as schema_file:
        try:
            schema = json.load(schema_file)
            validate(data, schema) 
        except Exception as e:
            print "Schema validation Error Message :",e.message 

It is giving error on terminal:

Schema validation Error Message : Expecting property name: line 19 column 5 (char 401)

Problem is simple: What would be the type format for timestamp used in above jsonschema? Please help

Upvotes: 2

Views: 671

Answers (1)

Anurag
Anurag

Reputation: 1013

Got your problem:-

You are giving type instead of format.

Following example is working for me:-

data = {"timestamp": "2013-09-16T15:40:16.21211"}

schema ="""{
    "type":"object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "http://jsonschema.net",
    "required":true,
    "properties":{
        "timestamp": { 
            "format" :"date-time",
            "type":"string",
            "required" :false
        }
     }}"""

jsonschema.validate(data,json.loads(schema))

Upvotes: 1

Related Questions