Shod
Shod

Reputation: 925

Apply same schema to a fixed set of properties

I have a JSON Schema like this:

{
    "$id": "<some id here>",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "settings": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "setting_1": {
                    "type": "string"
                },
                "setting_2": {
                    "type": "string"
                },
                "setting_3": {
                    "type": "string"
                }
            },
            "required": [
                "setting_1",
                "setting_2",
                "setting_3"
            ]
        }
    },
    "required": [
        "settings"
    ]
}

There are many more entries like setting_1, setting_2, etc. Is there a way in which I can apply the config:

{"type": "string"}

to all these properties efficiently? I know we can use $def, but then it will not bring about any considerable reduction in lines, as the common config is just a single key-value pair.

Thank you

Upvotes: 0

Views: 45

Answers (1)

Jeremy Fiel
Jeremy Fiel

Reputation: 3252

EDIT: use definitions for reusable schema

{
    "$id": "<some id here>",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "settings": {
            "type": "object",
            "properties": {
              "setting1": { "$ref": "#/definitions/StringType"},
              "setting2": { "$ref": "#/definitions/StringType"}
        }
      }
    },
    "required": [
        "settings"
    ],
    "definitions":{
      "StringType": {
        "type": "string"
      }
    }
}

This will give you an instance

{
  "settings": {
     "setting1": "foo",
     "setting2": "bar"
  }
}

Upvotes: 0

Related Questions