1

Continuing from my previous unresolved issue, I am using json-schema-faker and json-server and i am currently trying to reuse some data from my json schema.

I have found the property: jsonPath from this page in Github which explains exactly what I am trying to do and what my problem is.

I am trying to test this property from my JavaScript application which generates these data and also from the json-schema-faker site which is the library I use. Using this property using any of the above ways, returns a random string instead of the id I am referring to ( "jsonPath": "$..properties.test.items.properties.id")

enter image description here

Validating using this site, causes no issues and the value I am trying to re-use is picked up correctly using the jsonPath.

Is there anything I should import in my JavaScript code/mock data generator or what I am trying to do is not supported perhaps by the json-schema version?

Some of the paths I have tried to use are:

$..id $..test.items.properties.id $..test.id

This is my json schema:

{
  "title": "teest",
  "type": "object",
  "required": [
    "test"
  ],
  "properties": {
    "test": {
      "type": "array",
      "minItems": 1,
      "maxItems": 3,
      "uniqueItems": true,
      "items": {
        "type": "object",
        "required": [
          "id",
          "samples"
        ],
        "properties": {
          "id": {
            "type": "string",
            "enum": [
              "1,",
              "2",
              "3"
            ]
          },
          "samples": {
            "type": "array",
            "uniqueItems": true,
            "items": {
              "type": "object",
              "properties": {
                "test2": {
                  "type": "string",
                  "jsonPath": "$..properties.test.items.properties.id"
                }
              }
            }
          }
        }
      }
    }
  }
}

I would appreciate any help with this as I can't find anything online, and I really don't want to mock these data using hard-coded values.

Marialena
  • 817
  • 8
  • 31
  • I'm not sure what `jsonPath` does in the context of this library. It's not a standard JSON Schema keyword. Have you tried raising an issue on the *json-schema-faker* GitHub repo? You'll definitely get more people who are knowledgeable about that library there. – gregsdennis Mar 04 '19 at 18:36
  • I guess I should re-open the github issue I have posted here or open a new one as you suggested. Thank you for your response. – Marialena Mar 05 '19 at 06:50

1 Answers1

0

Maybe you have already found the solution to your problem but in case anyone else wants to know the solution:

Remove the type before jsonPath so instead of having

...
"items": {
    "type": "object",
    "properties": {
        "test2": {
            "type": "string", // Remove this line
            "jsonPath": "$..properties.test.items.properties.id"
        }
    }
}
...

change your schema to

...
"items": {
    "type": "object",
    "properties": {
        "test2": {
            "jsonPath": "$..properties.test.items.properties.id"
        }
    }
}
...

The json schema faker library does not know the type of your object you're trying to reference. Therefore, if you provide a type the json schmea faker will always override your relative value with a randomly generated type which is specified. I struggled with this as well because the documentation of the library is not provided (for the very most part) and not detailed enough unfortunately.

Alex
  • 221
  • 3
  • 11