Dem0n13
Dem0n13

Reputation: 825

Json-schema doesn't validate json with $ref-references

I have this json:

{
  "categories": [
    {
      "id": 1,
      "name": "cat1",
      "topics": [
        {
          "$ref": "#/topics/1"
        }
      ]
    }
  ],
  "topics": [
    {
      "id": 1,
      "name": "top1"
    }
  ]
}

And I've written the next schema to validate it:

{
  "definitions": {
    "category": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "topics": {
            "type": "array"
            "items": { "$ref": "#/definitions/topic" }
          }
        }
      },
      "topic": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          }
        }
      }
  },
  "type": "object",
  "properties": {
    "categories": {
      "items": { "$ref": "#/definitions/category" },
      "type": "array"
    },
    "topics": {
      "items": { "$ref": "#/definitions/topic" },
      "type": "array"
    }
  }
}

When I use this schema on popular online validators, it doesn't catch invalid references like #/topics/5 or #/ttt/555. Can I use this schema to validate references? Can you suggest me library or service, that can do it?

Upvotes: 0

Views: 2215

Answers (2)

Henry Andrews
Henry Andrews

Reputation: 693

Currently this is outside the scope of JSON Schema. The proposal @erosb mentions is still under consideration, but not for the soon-to-be-forthcoming draft-07. With enough demand it may be considered for draft-08. It would be a significant expansion of the project's scope, which is why it has been on hold while other things are addressed.

Some validators make it easy to define your own extension keywords, which may be a good way to do what you want. There are definitely libraries that will apply a JSON Pointer and let you find out if it points to anything or not. If you implement @erosb's proposal somewhere, it would be great if you could comment on the issue and let us know how it works out.

Upvotes: 1

erosb
erosb

Reputation: 3141

I'm not sure if I properly understand what you try to achieve. I assume you want to denote that the items of the "topics" array should be JSON references ("$ref" with a JSON Pointer) _and the pointed object should match the schema "#/definitions/topic".

If this is the case, then currently there is no way to express it with json schema, so - even with the latest version - you can only denote that a string should be a json pointer, but you can't make restrictions on what the type of the referenced object should be.

Last year I made a suggestion addressing this problem, but due to mixed feedback it got somewhat stuck.

Upvotes: 1

Related Questions