0

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"
            }
        }
    }
}
dotnetCarpenter
  • 10,019
  • 6
  • 32
  • 54
  • What do you mean by "a Map"? JSON only has the following... object, array, string, number, "true", "false", "null" – Relequestual Sep 03 '20 at 14:32
  • I think, besides the "Map" confusion, you're assuming that properties defined in `properties` also makes them required. This is not the case. – Relequestual Sep 03 '20 at 14:34
  • Map, HashTable, HashMap, Associated List, LinkedList. While not exactly the same, I thought that `Map` is the name of the data structure that is most well known in the JS world. E.i. key-value list. Perhaps `Dictionary` would be a better word? – dotnetCarpenter Sep 03 '20 at 20:00
  • Actually a 5 year old similar question came up when I searched for `Dictionary`. https://stackoverflow.com/q/27357861/205696 I will have to check tomorrow if the answer stil holds. – dotnetCarpenter Sep 03 '20 at 20:06
  • your top-level schema refers to properties/campaignType, but I don't see campaignType anywhere in your instance. I think maybe you want additionalProperties at the top level, but it's kind of unclear. – Ethan Sep 04 '20 at 00:10

0 Answers0