1

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"]
  }
}
2
  • is this helps? stackoverflow.com/questions/35683723/… Commented Jun 7, 2020 at 7:43
  • @Inga thanks for helping, but in that question, the key are "fixed". It's easy to define types for a fixed-keys object. Commented Jun 7, 2020 at 7:45

1 Answer 1

2

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

{
  "type": "object",
  "additionalProperties": {
    "type": ["string", "array"]
  }
}
Sign up to request clarification or add additional context in comments.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.