0

I am using fastjsonschema for validating json record against its schema. The compile method works fine for simple schemas but While compiling the json schema with references fastjsonschema is getting into error. TypeError: string indices must be integers. Below are the Code snippet, json schema and the error.

Code to compile the schema #####
import fastjsonschema

schema_file = open(schema.json)
schema_str = schema_file.read()
validatation = fastjsonschema.compile(schema_str)
Content of schema.json
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "organization",
    "description": "JSON schema",
    "type": "object",
    "properties": {
        "transactionDetail": {
            "id": "https://example.com/transactionDetail",
            "type": "object",
            "properties": {
                "transactionID": {
                    "description": "A number assigned by the calling application to uniquely identify this request.",
                    "type": "string"
                },
                "transactionTimestamp": {
                    "description": "The date and time when this request was submitted.",
                    "type": "string"
                }
            },
            "required": [
                "transactionID"
            ],
            "additionalProperties": false
        },
        "organization": {
            "$ref": "#/definitions/organization"
        }
    },
    "additionalProperties": false,
    "definitions": {
        "organization": {
            "type": "object",
            "properties": {
                "identifier": {
                    "description": "identification number.",
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 12
                },
                "countryCode": {
                    "description": "The two-letter country code.",
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 2
                },
                "timestamp": {
                    "description": "The date and time that the record was created.",
                    "type": "string"
                },

                "required": [
                    "identifier",
                    "countryCode"
                ],
                "additionalProperties": false
            }
        }
    }
}

Expected Result: Nothing

Actual Result:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/__init__.py", line 153, in compile
  resolver, code_generator = _factory(definition, handlers)
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/__init__.py", line 193, in _factory
  resolver = RefResolver.from_schema(definition, handlers=handlers)
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/ref_resolver.py", line 89, in from_schema
  **kwargs
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/ref_resolver.py", line 78, in __init__
  self.walk(schema)
  File "/dnbusr1/z-gda-pad/anaconda3/lib/python3.6/site-packages/fastjsonschema/ref_resolver.py", line 148, in walk
  elif '$ref' in node and isinstance(node['$ref'], str):
      TypeError: string indices must be integers

1 Answers1

1

I struggled with this problem too, and it seems you need to feed the fastjsonschema compile and validate methods with a dict (instead of a str).

One way to do this easily is by using the json module in Python:

with open(myschema, 'r') as file:
  schemadict = json.loads(file.read())
  validate = fastjsonschema.compile(schemadict)
M_breeb
  • 195
  • 1
  • 9