Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python jsonschema validation using schema list

I'm trying to validate a json file against a schema using python and jsonschema module. My schema is made up from a list of schemas, one of them has definitions of basic elements and the rest are collections of these elements and other objects.

I can't find the documentation for function which loads the list of schemas so that I can validate using it. I tried separating schemas into a dictionary and calling the appropriate one on a jsonObject, but that doesn't work since they cross reference each other.

How do I load/assemble all schemas into one for validation?

Part of the schema I'm trying to load:

[{
    "definitions": {
     "location": {
       "required": [
         "name",
         "country"
       ],
       "additionalProperties": false,
       "properties": {
         "country": {
           "pattern": "^[A-Z]{2}$",
           "type": "string"
         },
         "name": {
           "type": "string"
         }
       },
       "type": "object"
      }
    },
    "required": [
       "type",
       "content"
    ],
    "additionalProperties": false,
    "properties": {
     "content": {
      "additionalProperties": false,
      "type": "object"
     },
     "type": {
      "type": "string"
     }
    },
    "type": "object",
    "title": "base",
    "$schema": "http://json-schema.org/draft-04/schema#"
  },
  {
    "properties": {
      "content": {
        "required": [
          "address"
        ],
        "properties": {
          "address": {
            "$ref": "#/definitions/location"
        }
      },
      "type": {
        "pattern": "^person$"
      }
    }
  }]

And the json object would look something like this:

{
 "type":"person",
 "content":{
  "address": {
   "country": "US",
   "name" : "1,Street,City,State,US"
  }
 }
}
like image 897
Rizhiy Avatar asked Sep 12 '26 02:09

Rizhiy


1 Answers

You can only validate against one schema at a time, but that schema can reference ($ref) external schemas. These references are usually URIs that can be used to GET the schema. A filepath might work too if your schemas are not public. Using a fixed up version of your example, this would look something like this ...

http://your-domain.com/schema/person

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Person",
  "allOf": [{ "$ref": "http://your-domain.com/schema/base#" }],
  "properties": {
    "type": { "enum": ["person"] },
    "content": {
      "properties": {
        "address": { "$ref": "http://your-domain.com/schema/base#/definitions/location" }
      },
      "required": ["address"],
      "additionalProperties": false
    }
  }
}

http://your-domain.com/schema/base

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "base",
  "type": "object",
  "properties": {
    "content": { "type": "object" },
    "type": { "type": "string" }
  },
  "required": ["type", "content"],
  "additionalProperties": false,
  "definitions": {
    "location": {
      "type": "object",
      "properties": {
        "country": {
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        },
        "name": { "type": "string" }
      },
      "required": ["name", "country"],
      "additionalProperties": false
    }
  }
}

Some documentation that might be useful

  • https://python-jsonschema.readthedocs.org/en/latest/validate/#the-validator-interface
  • https://python-jsonschema.readthedocs.org/en/latest/references/
like image 183
Jason Desrosiers Avatar answered Sep 14 '26 15:09

Jason Desrosiers