Search code examples
jsonschemajson-schema-validatorpython-jsonschema

How to ref JSON schema definition of type array with anyOf definition in other files


I have a jsonschema definition definition-1.json like below. I have a property called inputs of type array which refers anyOf definitions like

"inputs": {
      "type": "array",
      "items": {
        "anyOf": [
          {
            "$ref": "#/definitions/stringParameter"
          },
          {
            "$ref": "#/definitions/numberParameter"
          },
          {
            "$ref": "#/definitions/booleanParameter"
          },
          {
            "$ref": "#/definitions/jsonParameter"
          },
          {
            "$ref": "#/definitions/fileParameter"
          }
        ]
      }
    }

Then the parameter definitions defined like below in the same jsonschema definition-1.json

"definitions": {
    "stringParameter": {
      "type": "object",
      "required": [
        "name",
        "description",
        "datatype"
      ]
   }
 ...
}

Now I want to reference this parameter definitions in my other schema dummy-1.json like below

  {
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "dummy-1.json",
  "allOf": [
    {
      "type": "object",
      "required": [
        "definition_data"
      ],
      "properties": {
        "definition_data": {
          "type": "object",
          "required": [
            "inputs"
          ],
          "properties": {
            "type": "array",
            "inputs": {
              "items": {
                "allOf": [
                  {
                    "prop1": {
                      "$ref": "#/definitions/stringParameter"
                    }
                  },
                  {
                    "prop2": {
                      "$ref": "#/definitions/numberParameter"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    {
      "type": "object",
      "properties": {
        "definition_data": {
          "$ref": "definition-1.json"
        }
      }
    }
  ]
}

This doesn't looks like working. prop1 will validate successfully for any properties part of other parameter definition too , even though in dummy-1.json I explicitly referred #/definitions/stringParameter . I can understand inputs in definition-1.json accepts anyOf all parameter definitions. But want to know how we can achieve 1:1 parameter definition mapping for dummy-1.json .


Solution

  • If my understanding is correct, you want to refer from dummy-1.json to subschemas defined in definitions-1.json. To do that, you have to specify the URI of definition-1.json and append the pointer of the subschema in it, like:

    dummy-1.json:

    {
      "$ref": "uri-of-definitions-1.json#/definitions/stringParameter"
    }
    

    Note that the URI to be set is quite specific to the json schema library you are using. Usually an absolute https:// URL works, but it isn't always convenient to work with. Maybe a relative path can work for you as well (like ./definition-1.json#/definitions/stringParameter)