JSON Schema object contains value

Viewed 160

I want to check that "obj" contains at least one value "test". But keys in the "obj" are random.

Json:

{
  "title": "doc",
  "obj": {
   "xxxx-random": "test",
   "zzzz-random": "wol"
  }
}

My idea was to use oneOf but it doesn't work as I expected:

Schema Json:

{
  "properties": {
    "title": {
      "type": "string"
    },
    "obj": {
      "type": "object",
      "oneOf": [
        {
          "contains": {
            "properties": {
              "type": "string",
              "desc":"test"
            }
          }
        }
      ]
    }
  }
}
1 Answers

You can do that with slightly-mindbending uses of the "not" keyword. That is, we can construct a schema that asserts:

  • the property "obj" must exist
  • "obj"'s value is an object, and there must be at least one property
  • and it cannot be true that all properties under "obj" do NOT match "test"
{
  "type": "object",
  "required": ["obj"],
  "properties": {
    "obj": {
      "type": "object",
      "minProperties": 1,
      "not": {
        "additionalProperties": { "not": { "const": "test" } }
      }
    }
  }
}
Related