Search code examples
jsonschema

JSON-schema exclude properties (opposite to required properties)


in JSON-schema, one can require certain keys in an object, see this example, taken from the json schema docs:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "email": { "type": "string" },
    "address": { "type": "string" },
    "telephone": { "type": "string" }
  },
  "required": ["name", "email"]
}

I need the opposite: Is there a way to forbid or prevent a certain key from being in an object? Specifically, I want to prevent users from having an empty key inside an object:

{
    "": "some string value"
}

Solution

  • You can exclude certain keys by name with:

    {
     "not": {
      "anyOf": [
        { "required": [ "property1" ] },
        { "required": [ "property2" ] },
        { "required": [ "property3" ] },
        ...
      ]
    }
    

    https://json-schema.org/understanding-json-schema/reference/combining.html