Question

So I have one json schema and within this schema I am referencing another two schemas within the same json file, this works fine.

{
  "id": "http://ourdns.co.za/public/assets/json/formSchema.json",
  "type": "object",
  "properties": {
    "person": {
      "type": "object",
      "id": "#person",
      "properties": {
        "first_name": {
          "title": "What is your first name",
          "type": "string"
        },
        "last_name": {
          "title": "What is your last name",
          "type": "string"
        }
      }
    },
    "person_api": {
      "type": "object",
      "id": "#person"
    }
  }
}

What i would like to have is a root json schema that references two other json schemas that are external to the root. This differs from my current schema where I have all the schemas in one file(not ideal). There is a slight problem in that I cannot use $ref as a reference keyword because the plugin we are using does not support this. However we have found that id can be used as a reference keyword.(JsonForm is the plugin). How would we then get these using the id keyword because it does not seem to work?

{
  "id": "http://ourdns.co.za/public/assets/json/formSchema.json",
  "type": "object",
  "properties": {
    "person_api": {
      "type": "object",
      "id": "public/assets/person.json"
    }
  }
}

1) How do we call the same data externally, like.. "id": "public/assets/person.json" instead of combining it all in one file? 2) How would we retrieve particular properties for example if we only need person.firstname from person.json schema?

{
  "id": "http://dsn.co.za/public/assets/json/person.json",
  "type": "object",
  "properties": {
    "first_name": {
      "title": "What is your first name",
      "type": "string"
    }
  }
}
Was it helpful?

Solution

You cannot perform referencing with just id on its own. For referencing, you have to use $ref.

The id keyword allows you to supply a URL for the schema as a target for referencing:

{
    "id": "http://example.com/schemas/example",
    "type": "object",
    "properties": {
        "arr1": {
            "id": "#item-schema",
            ...
        },
        "arr2": {"$ref": "#item-schema"}
    }
}

This is so that you can reference schemas using nice URLs (e.g. http://example.com/schemas/example#item-schema) instead of using JSON Pointer fragment syntax. It also allows you to re-organise your schema (e.g. move the item schema into definitions) without changing the URL.

However, for the referencing itself, you still need to use $ref. If you need this functionality, you need support for it in whatever tool you're using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top