skip to Main Content

Is there a way to enforce the structure of an object setting?

For example package.json:

{
  "contributes": {
    "configuration": {
      "properties": {
        "myextension.mysetting": {
          "type": "object",
          "default": {
            "anyKey": {
              "property1": "",
              "property2": ""
            }
          }
        }
      }
    }
  }
}

The setting myextension.mysetting is an object type, where key anyKey could be any string at user’s choosing and property1 and property2 are the only acceptable properties in that object. If user attempt enter other property name (i.e. property3) it should show it as not an acceptable property.

Is this possible?

2

Answers


  1. Chosen as BEST ANSWER

    With @rioV8 answer this is the solution I was looking for:

    {
      "contributes": {
        "configuration": {
          "properties": {
            "myextension.mysetting": {
              "type": "object",
              "patternProperties":{
                "^[a-zA-Z0-9]+$": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "property1": {"type": "string"},
                    "property2": {"type": "string"}
                  }
                }
              },
              "additionalProperties": false,
              "default": {
                "anyKey": {
                  "property1": "",
                  "property2": ""
                }
              }
            }
          }
        }
      }
    }
    

  2. you can add the property

    "additionalProperties": false
    

    to the scheme where you define property1and property2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search