I am attempting to use the jsonschema and python_jsonschema_objects libraries to create a python object from a schema file, populate some data in that object, and then validate it against the original schema. Somehow I think i'm doing something wrong but not sure what exactly.

I've attempted several different schemas and data values as well as removing arrays, using flat/single object. Still the validation fails.

from jsonschema import validate import python_jsonschema_objects as pjs import jsonschema import json import os with open('geocoordinate/geocoordinatearray3.schema.json') as opfile: schema = json.load(opfile) builder = pjs.ObjectBuilder(schema) ns = builder.build_classes() Coordinate = ns.Rootschema ca = Coordinate(latitude=22.22,longitude=33.33) print(ca.serialize()) try: print("about to validate first example") validate(instance=ca, schema=schema) except jsonschema.exceptions. ValidationError as e: print("this is validation error:", e) except json.decorder.JSONDecodeError as e: print("not JSON", e) 

this is the schema file:

 { "definitions": {}, "$schema": "", "$id": "", "type": "object", "title": "rootSchema", "required": [ "latitude", "longitude" ], "properties": { "location": { "$id": "#/properties/location", "type": "string", "title": "The Location Schema", "default": "", "examples": [ "Denver, CO" ], "pattern": "^(.*)$" }, "latitude": { "$id": "#/properties/latitude", "type": "number", "title": "The Latitude Schema", "default": 0.0, "examples": [ 39.7392 ] }, "longitude": { "$id": "#/properties/longitude", "type": "number", "title": "The Longitude Schema", "default": 0.0, "examples": [ -104.9903 ] }, "alt": { "$id": "#/properties/alt", "type": "integer", "title": "The Alt Schema", "default": 0, "examples": [ 5280 ] } } } 

I'm expecting this to validate, it's pretty straightforward what i'm attempting to do. Getting this error:

about to validate first example this is validation error: 0> latitude= 22.22> location= > longitude= 33.33>> is not of type 'object'

Failed validating 'type' in schema:

schema

On instance:

<rootschema alt=<Literal<int> 0> latitude=<Literal<float> 22.22> location=<Literal<str> > longitude=<Literal<float> 33.33>> 
4

2 Answers

I got it working, the issue was the typing. I had to take the python instance and serialize() it. I think it's a string type by default. Thanks for the help everybody!

I encountered the same issue and used json.loads on the json String before validation :

import json
json.loads('{"property1":"value1"}')

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy