1

I would like to create a json schema (draft-06) based on the following settings object:

"settingData" : {
  "$id": "#settingData",
  "oneOf" : [
    { "type"  : "number" },
    { "type" : "array", "items" : { "oneOf" : [ { "type" : "number"},{ "type" : "null" } ] } },
    { "type" : "array", "items" : { "$ref" : "#setting" } }
  ]
},
"setting" : {
  "$id": "#setting",
  "type" : "object",
  "properties" : {
    "name": {
      "type": "string",
      "title": "type of setting",
      "examples": [
        "led-brightness"
      ]
    },
    "data": {
      "$ref" : "#settingData"
    }
  },
  "required" : ["type","data"]
},

I would like to create a specialization of this object, that has a settings name "status-ranges" and and contains a subset of the above definitions. How can I get my above type "settings" to not apply, when name is "status-ranges". I thought about using pattern, but regex patterns that match everything except a certain string seem a bit special, I tried one and it did not work. Then there is the not keyword but I am still looking for examples how that can be applied. Any Ideas ?

Thomas
  • 311
  • 2
  • 17
  • This seems to be relevant to https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required – vearutop May 21 '18 at 01:52

1 Answers1

0

It worked for me using the regex: "pattern": "^((?!status-ranges).)*$" in the name property of setting. This excludes the string "status-ranges from the allowed names. The 'specialized' setting then has a const name "status-ranges", as follows:

"settingData" : {
  "$id": "#settingData",
  "oneOf" : [
    { "type"  : "number" },
    { "type" : "array", "items" : { "oneOf" : [ { "type" : "number"},{ "type" : "null" } ] } },
    { "type" : "array", "items" : { "$ref" : "#setting" } }
  ]
},
"setting" : {
  "$id": "#setting",
  "type" : "object",
  "properties" : {
    "name": {
      "type": "string",
      "pattern": "^((?!status-ranges).)*$",
      "title": "type of setting",
      "examples": [
        "led-brightness"
      ]
    },
    "data": {
      "$ref" : "#settingData"
    }
  },
  "required" : ["type","data"]
},
"statusRange" : {
  "$id": "#statusRange",
  "type" : "object",
  "properties" : {
    "name": {
      "const": "status-ranges"
    },
    "data": {
      "$ref" : "#settingData"
    }
  },
  "required" : ["type","data"]
},

I would still like to find out how "not" works in JSON Schema though.

Thomas
  • 311
  • 2
  • 17
  • Change `"pattern": "^((?!status-ranges).)*$"` to `"not":{"enum":["status-ranges"]}`. Or with `draft-06` and later it can be `"not":{"const":"status-ranges"}`. Validator will check that value is NOT valid against `not` subschema which is defined as constant value. – vearutop May 22 '18 at 03:24