java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.62k stars 399 forks source link

How to reference other json file by using $ref? #299

Open Tryking opened 5 years ago

Tryking commented 5 years ago

How to reference other json file by using $ref?

{
  "level" : "error",
  "message" : "string is not a valid URI: \"file://C:\\Users\\Tryking\\Desktop\\JsonSchema\\AreaTest.json\"",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/area"
  }
dheepakji commented 5 years ago

Use $ref as below to refer the external json file:

schema.json

{
  "type": "object",
  "properties": {
    "employee": {
      "$ref": "file:/Users/apiAutomation/schema/definitions.json#/definitions/employeeDetails"
    }
  }
}

Use file attribute and give the absolute path of your external json file. absolute path of my file : /Users/apiAutomation/schema/definitions.json Note: the path following the #(pound) symbol is the path of my employeeDetails in my definitions.json

definitions.json

{
  "definitions": {
    "employeeDetails": {
      "$id": "#employeeDetails",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "age": {
          "type": "number"
        },
        "id": {
          "type": "number"
        }
      },
      "required": [
        "name",
        "id"
      ]
    }
  }
}
Tryking commented 5 years ago

Use $ref as below to refer the external json file:

schema.json

{
  "type": "object",
  "properties": {
    "employee": {
      "$ref": "file:/Users/apiAutomation/schema/definitions.json#/definitions/employeeDetails"
    }
  }
}

Use file attribute and give the absolute path of your external json file. absolute path of my file : /Users/apiAutomation/schema/definitions.json Note: the path following the #(pound) symbol is the path of my employeeDetails in my definitions.json

definitions.json

{
  "definitions": {
    "employeeDetails": {
      "$id": "#employeeDetails",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "age": {
          "type": "number"
        },
        "id": {
          "type": "number"
        }
      },
      "required": [
        "name",
        "id"
      ]
    }
  }
}

I see, thank you.

jillepally-nagender commented 9 months ago

But what if i want to give relative path , how to give that