JSONSchema splits one large schema file into multiple smaller logical files

I want the common parts of the json schema to be written to a file and then referenced this file from the main schema file. Thus, basically instead of 1 large json schema file, there are several files that reference each other. I am using json-schema-validator lib for validation.

eg:.

$ 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" } } } } 
+4
source share
2 answers

The following solution worked for me:

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

The above solution satisfies my requirements:

  • All of my schema files are located on the local file system and are not hosted by any URL
  • The specified path refers to the directory in which I run the mvn targets.
+6
source

Here is how I did it:

Given the following file structure from the root of the 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" } } } } 

Confirmed using JsonValidator.java

0
source

Source: https://habr.com/ru/post/1498342/


All Articles