jsonjsonschemajson-schema-validator

JSON Schema - Foreign Key (FK) Validation


I have the JSON schemas below:

Schema A:

{
  "$schema": "http://json-schema.org/draft-04/hyper-schema#",
  "title": "A",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "entityBId": {
      "type": "string"
    }
  },
  "required": [
    "name",
    "entityBId"
  ],
  "links": [
    {
      "rel": "B",
      "href": "myapi/EntityB?id={entityBId}"
    }
  ]
}

Schema B :

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "B",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    }
  },
  "required": [
    "name"
  ]
}

I'm trying to figure out if there is a way to run something like a integrity check based in a JSON Schema with external links/references. For instance: When I receive an Object A with a entityBId = 1, I'd like to fetch this entity B running a GET in the endpoint declared in the links href and check if there is a valid object from the received id. It will run like a deep validation and can be useful in a scenario without a defined DB schema.


Solution

  • As suggested by Raj Kamal, I made my own "link validation". There is a function that looks for link attribute on schemas and validate the externals references directly on database and throw an exception if a valid reference is not found.