Home > Back-end >  A setting type as object with predefined structure
A setting type as object with predefined structure

Time:10-23

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?

CodePudding user response:

you can add the property

"additionalProperties": false

to the scheme where you define property1and property2

CodePudding user response:

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": ""
            }
          }
        }
      }
    }
  }
}
  • Related