How would you design JSON Schema for an arbitrary key?

On json-schema.org you will find something appropriate in the File System Example section. You can define patternProperties inside an object.

{
    "type": "object",
    "properties": {
        "/": {}
    },
    "patternProperties": {
        "^(label_name_[0-9]+)+$": { "type": "integer" }
    },
    "additionalProperties": false,
 }

The regular expression (label_name_[0-9]+)+ should fit your needs. In JSON Schema regular expressions are explicitly anchored with ^ and $. The regular expressions defines, that there has to be at least one property (+). The property consists of label_name_ and a number between 0 and 9 whereas there has to be at least one number ([0-9]+), but there can also arbitrary many of them.

By setting additionalProperties to false it constrains object properties to match the regular expression.

Leave a Comment