12

I want the common parts of json schema to be captured in a file and then reference this file from the main schema file. So basically instead of 1 big json schema file, multiple files which reference each other. Am using json-schema-validator lib to validate.

E.g.:

$ ls schemas/
response_schema.json results_schema.json

$ cat schemas/response_schema.json
{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "required": [ "results" ],
    "properties": {
        "results": "####Reference results_schema.json file here somehow####"
    }
}   

$ cat schemas/results_schema.json
{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "array",
    "items": {
        "type": "object",
        "required": ["type", "name"],
        "properties": {
            "name": { "type": "string" },
            "dateOfBirth": { "type": "string" }
        }
    }
}
nishant
  • 955
  • 2
  • 11
  • 27
  • Have a look at the [JSON Schema: core definitions and terminology](http://tools.ietf.org/html/draft-zyp-json-schema-04#section-3.4) – atomman Aug 22 '13 at 09:42
  • Thx atomman. Digging more found partial answer. `"results": { "$ref": "file://localhost/c:/eclipse-workspaces/myproject/src/test/resources/schemas/results.json" }` Still don't know how to give relative paths though! – nishant Aug 22 '13 at 10:04
  • Try pushing it to your server, and use `./resources/schemas/results.json` as referance. I don't know if this will work, but you should at least see the full url in your console as the library tries to resolve it. – atomman Aug 22 '13 at 10:44

2 Answers2

15

Following solution worked for me:

    "results": {
        "$ref": "file:src/test/resources/schemas/results.json"
    }

The above solution satisfies my requirements:

  1. All my schema files are on local file system and not hosted by some url
  2. Path specified is relative to the directory where I run mvn goals.
nishant
  • 955
  • 2
  • 11
  • 27
  • There is no reason that references can't be relative. :) They are resolved relative to the current schema path (or "id", if that is defined instead). – cloudfeet Sep 10 '13 at 15:41
  • For me this did not work at all. I've tried also some alternatives and double checked them. – Flowryn Jun 22 '17 at 15:33
1

This is how I have done it:

Given the following file structure from the root of your application:

/schemas/
         response_schema.json
         results_schema.json

response_schema.json:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "resource:/schemas/response_schema#",
    "type": "object",
    "required": [ "results" ],
    "properties": {
        "results": {
          "type": "object",
           "$ref": "results_schema.json"
     }
}  

results_schema.json:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "id": "resource:/schemas/results_schema#",
    "type": "array",
        "items": {
            "type": "object",
            "required": ["type", "name"],
        "properties": {
            "name": { "type": "string" },
            "dateOfBirth": { "type": "string" }
        }
    }
}

Have validated with JsonValidator.java

Mangusta
  • 443
  • 4
  • 4