JSON Schema - How to limit an object to specific name, with value type 'boolean', without using regex

Viewed 27

I want to record data from checkbox inputs, for example:

{
  client 1: false,
  client 2: true,
  client 3: false,
}

or

{
 dogs: true
 cats: true
 horses: false
}

I want to create simple JSON Schemas for these, and would prefer to use a system where I don't have to learn/type/read complicated regex. The following works quite nicely for example 1:

      "type": "object",
      "patternProperties": {
        "^Client [1-3]$": {
          type: "boolean",
        },
      },
      "additionalProperties": false,

Unfortunately, it will be complicated when the object properties don't follow a simple pattern, as in example 2. Is there a way to strictly specify the object properties in an array, and also specify the value type? I want to limit the object property names only to the array. So I want something like this pseudo schema:

   "additionalProperties": false,
   "propertyNames": ["Client 1", "Client 2", "Client 3"],
   "allValuesToBe": "boolean"

(I made the last keyword up, but want something like that.) This would accept/reject the following:

  client 1: false, // accept
  foobar: true, // reject, bad prop name
  client 3: 123, // reject, bad value
1 Answers

additionalProperties doesn't have to be a boolean - you can put a normal schema there. So as long as all your properties in this object follow the same guidelines, you can do:

"propertyNames": { "enum": ["Client 1", "Client 2", "Client 3"] },
"additionalProperties": { "type": "boolean" }
Related