kaixiang1212
kaixiang1212

Reputation: 75

JSON Schema: Restrict additionalProperties to exactly one type

I am trying to validate an object with all the additionalProperties having the same exact type.

Here's my attempt on the JSON Schema:

{
  additionalProperties: {
    oneOf: [
      { type: "string" },
      { type: "boolean" },
    ]
  }
}

but this only checks for the individual additionalProperties.

Is there a way to achieve the following?

// Valid
{
  "a": true
  "b": false
}

{
  "c": "c",
  "d": "d"
}

// Invalid
{
  "a": true,
  "b": "b"
}

Upvotes: 2

Views: 411

Answers (1)

sabasabo
sabasabo

Reputation: 119

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
   "oneOf": [
      {
        "additionalProperties": {
          "type": "string"
        }
      },
      {
        "additionalProperties": {
          "type": "boolean"
        }
      }
    ]
}

Working:

{
  "a": true,
  "b": false
}

Not working:

{
  "a": true,
  "b": "false"
}

Upvotes: 3

Related Questions