6

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": "http://json-schema.org/draft-07/schema#",
      "$id": "http://example.com/root.json",
      "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>>
Tim B
  • 215
  • 1
  • 2
  • 12
  • Did you create the schema file yourself, or was it provided to you? – John Gordon Aug 13 '19 at 19:07
  • I used: https://jsonschema.net/ i plugged in some sample data and used it. i've tried a couple different schemas, but keep getting the same error. i feel like i'm doing something wrong in the python code but not sure – Tim B Aug 13 '19 at 19:20
  • Show us sample data that you used to validate schema? – lahiruk Aug 14 '19 at 03:53
  • 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! – Tim B Aug 14 '19 at 14:23

2 Answers2

2

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!

Tim B
  • 215
  • 1
  • 2
  • 12
1

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

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

SwissFr
  • 178
  • 1
  • 12