2

I am working with a JsonSchema that utilizes nested data structures where I use $ref for some properties and for others I just write in the data structures (no $ref). Basically I have to consider many types of JsonSchema Structures.

What I am trying to do is, given a list of Json Pointers, I want to validate that all the Json Pointer Expressions are valid properties existing in the Schema. The Json Pointer Expressions can be nested as well. Here is an example:

product.schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": 
    "dimensions": {
      "type": "object",
      "properties": {
        "length": {
          "type": "number"
        },
        "width": {
          "type": "number"
        },
        "height": {
          "type": "number"
        }
      }
    },
    {
    "warehouseLocation": {
      "description": "Coordinates of the warehouse where the product is located.",
      "$ref": "https://example.com/geographical-location.schema.json"
    }
  }
}

geographical-location.schema.json

{
  "$id": "https://example.com/geographical-location.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Longitude and Latitude",
  "description": "A geographical coordinate on a planet (most commonly Earth).",
  "type": "object",
  "properties": {
    "latitude": {
      "type": "number"
    },
    "longitude": {
      "type": "number"
    }
  }
}

The above JsonSchema has the two types of nested schema structures. Lets say give these Schema, I also am give a list of JsonPtrExps:

["/dimensions/length", "/warehouseLocation/longitude", "/dimensions/mass"]

I am trying to find a way to use the schema to predict and validate which of the JsonPointerExpressions would return a valid property. "/dimensions/length", "/warehouseLocation/longitude" would pass this validation but "/dimensions/mass" would not.

Also I would like to be able to make this decisions went I am only reading the product.schema.json even though I can access geographical-location.schema.json by using the $ref field.

Any thoughts?

  • Why do you want to validate the JSON against the schemas yourself instead of using a JSON schema validation library that would do the job for you? – Matteo NNZ Aug 18 '22 at 20:09
  • @MatteoNNZ It's a little more complicated but basically I need to allow users of the schema to attach a list of JsonPointerExpressions that will be used to carry out a function on JsonData that reflects the JsonSchema, but I need to prevalidate that the JsonPointerExpressions are valid properties defined by the JsonSchema, does that make sense? – Juan Rodriguez Aug 18 '22 at 20:23
  • Not entirely, but in that case wouldn't you simply want to parse the JSON schema as a simple JSON node and validate the paths as if they were standard JSON paths? – Matteo NNZ Aug 18 '22 at 20:40
  • Yes but the obstacle is dealing with $ref cases. – Juan Rodriguez Aug 18 '22 at 20:42
  • JSON Schemas are (as you have shown) generally represented simply in JSON. Evaluating a JSON Pointer against a JSON blob shouldn't be a great exercise. There are probably libraries in Java that do this for you. For example, [here's mine in .Net](https://www.nuget.org/packages/JsonPointer.Net) and [Microsoft's](https://www.nuget.org/packages/Microsoft.Json.Pointer). – gregsdennis Aug 18 '22 at 20:43
  • Okay I kind of see. And how you want to handle the refs? I guess you want to load the referenced JSON schema and apply the paths there? – Matteo NNZ Aug 18 '22 at 20:43
  • JSON Pointers, even during evaluation of a schema, can't see through `$ref`s. They're just location identifiers in the raw JSON structure. – gregsdennis Aug 18 '22 at 20:44
  • I am thinking of writing some logic to just manually check through $ref, I just need to find out how json validation does that – Juan Rodriguez Aug 18 '22 at 22:16
  • Are you expecting users to provide JSON Pointers which point through references as if the references had been in-lined? If so, this is not good. It's not always possible to in-line references, for example if they are recurisve. – Relequestual Sep 01 '22 at 08:35
  • Oh, you want to run the validate the JSON Pointers might be valid against the data validated by the JSON Schema? Interesting. Unless your schema is a lot more locked down (requires properties, prevents additional properties), you'll only be guessing. I've never seen anyone ask for such a tool, but you'd probably want to start by using a tool which creates an AST from the schema. You may find this to be a complex task. – Relequestual Sep 01 '22 at 08:38

0 Answers0