JSONSchema split one big schema file into multiple logical smaller files
10,480
Solution 1
Following solution worked for me:
"results": {
"$ref": "file:src/test/resources/schemas/results.json"
}
The above solution satisfies my requirements:
- All my schema files are on local file system and not hosted by some url
- Path specified is relative to the directory where I run mvn goals.
Solution 2
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
Related videos on Youtube
Author by
nishant
Updated on September 15, 2022Comments
-
nishant over 1 year
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" } } } }
-
cloudfeet over 10 yearsThere 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).
-
Flowryn almost 7 yearsFor me this did not work at all. I've tried also some alternatives and double checked them.
-
Pablo Pazos almost 4 yearsshow how the validation was done, the answer is incomplete, how do you use the JsonValidator class in this context?