I need to validate a JSON file which is structured as a Dictionary (key-value list) on the root level.
The JSON file was initially a one-off, 5 years ago but unfortunately the entire web application is now build upon that JSON file and it would be a non-trivial job to change the structure at this point.
The structure is as following:
{
"jetpack": {
"name": "jetpack",
"title": "Jetpack",
"description": "",
"currency": "eur",
"startDate": "2020-08-10",
"goal": 100,
...
},
"chile": {
"name": "chile",
...
},
...
}
The ...
represent more of similar data.
I came up with the following schema but it turns out that it does not validate the JSON file at all, using https://github.com/ajv-validator/ajv-cli version 3.2.1. The result is always valid JSON, even when "properties"
are missing.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Campaigns",
"type": "object",
"properties": {
"campaignType": { "#ref": "#/definitions/campaign" }
},
"definitions": {
"campaign": {
"type": "object",
"required": [ "endDate", "goal", "byline" ],
"properties": {
"endDate": {
"type": "string",
"format": "date",
"description": "E.g. 2020-07-26"
},
"goal": {
"type": "number",
"exclusiveMinimum": 0
},
"byline": {
"type": "string"
}
}
}
}
}
None of the examples I have found on SO or http://json-schema.org/learn/ has a Dictionary at the root level.
EDIT
The Q/A is here: Dictionary-like JSON schema.
Basically, using definitions
, #ref
and properties
was wrong. The answer is to use additionalProperties
only.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Campaigns",
"description": "Static data for all of our campaigns. Is also used to seed our DB.",
"type": "object",
"additionalProperties": {
"type": "object",
"required": [ "endDate", "goal", "byline" ],
"properties": {
"endDate": {
"type": "string",
"format": "date",
"description": "E.g. 2020-07-26"
},
"goal": {
"type": "number",
"exclusiveMinimum": 0
},
"byline": {
"type": "string"
}
}
}
}