gremo
gremo

Reputation: 48899

How to constraint values of an object in JSON schema?

I want to constrain my object values. In particular, only string and array (of string) types are allowed:

"myobject": {
  "string": "foo",
  "array": [
    "bar",
    "baz",
  ],
  "bool": true, // invalid
  "object": {}, // invalid
}

Object keys should be "free". I can't find a way to do this in JSON schema.

The following seems not working, it still allow bool and object value types:

"myobject": {
  "type": "object",
  "properties": {
    "type": ["string", "array"]
  }
}

Upvotes: 0

Views: 643

Answers (1)

Carsten
Carsten

Reputation: 2147

If the keys are „free“ as you put it, you can use the additionalProperties keyword like this:

{
  "type": "object",
  "additionalProperties": {
    "type": ["string", "array"]
  }
}

Upvotes: 1

Related Questions