additionalProperties with anyOf?

63 views
Skip to first unread message

Nilesh Akhade

unread,
Jan 25, 2017, 2:03:45 AM1/25/17
to JSON Schema

Schema should validate following objects:

{
  "md5": "11111111111111111111111111111111",
  "status": "malicious",
  "malware_type": "adware"
}

{
  "md5": "00000000000000000000000000000000",
  "status": "clean"
}

No additional property should be allowed.

Schema 1:
{
  "type": "object",
  "properties": {
    "md5": {
      "type": "string"
    }
  },
  "additionalProperties":false,
  "oneOf": [
    {
      "properties": {
        "status": {
          "type": "string",
          "enum": [
            "clean",
            "unknown"
          ]
        }
      }
    },
    {
      "properties": {
        "status": {
          "type": "string",
          "enum": [
            "malicious"
          ]
        },
        "malware_type": {
          "type": "string"
        }
      }
    }
  ]
}

Unfortunately it does not allow "status" property because : Property 'status' has not been defined and the schema does not allow additional properties


Schema 2:

{
  "type": "object",
  "additionalProperties":false,
  "properties": {
    "md5": {
      "type": "string"
    },
    "status": {
      "type": "string"
    },
    "malware_type": {
      "type": "string",
      "enum":["clean", "malicious", "unknown"]
    }
  },
  "dependencies": {
    "malware_type": {
      "properties": {
        "status": {
          "enum": [
            "malicious"
          ]
        }
      }
    }
  }
}

This schema does not invalidates
{
  "status": "malicious"
}

Nilesh Akhade

unread,
Jan 25, 2017, 2:04:42 AM1/25/17
to JSON Schema
Using draft v4

Henry Andrews

unread,
Jan 27, 2017, 11:17:45 AM1/27/17
to JSON Schema
Hi Nilesh,
  There's no way around this in Draft 04 without duplicating some of the schema to get "additionalProperties" behaving the way you want.  In the forthcoming Draft 06, "propertyNames": {"enum": [...]} gives you another way to specify a set of legal properties, although you still end up repeating the property names at least once.

thanks,
-henry
Reply all
Reply to author
Forward
0 new messages