Is there a way to raise an error for any additional key present in JSON if I am using the if-else condition of JSON Schema?

烂漫一生 提交于 2020-05-15 07:59:05

问题


I have a use case where I want to check the keys present in JSON, depending on the value of a different key.

Example JSON-1:

{ 
  "key_name" : "value1",
  "foo" : "random_value1"
}

Example JSON-2:

{ 
  "key_name" : "value2",
  "bar" : "random_value2"
}

As per these examples,

Rule 1. If the value of "key_name" is "value1" then only "foo" key should be present in JSON.

Rule 2. If the value of "key_name" is "value2", then only "bar" key should be present in JSON.

I have written the following JSON Schema for validating these JSON:

{
  "type": "object",
  "properties": {
    "key_name": {
      "type": "string",
      "enum": [
        "value1",
        "value2"
      ]
    },
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    }
  },
  "required": [
    "key_name"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "key_name": {
            "enum": [
              "value1"
            ]
          }
        }
      },
      "then": {
        "required": [
          "foo"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "key_name": {
            "enum": [
              "value2"
            ]
          }
        }
      },
      "then": {
        "required": [
          "bar"
        ]
      }
    }
  ]
}

Now, as per the rules, the following JSON's are invalid, and should raise an error.

{ 
  "key_name" : "value1",
  "foo" : "random_value1",
  "bar" : "random_value2"
}

OR

{ 
  "key_name" : "value2",
  "bar" : "random_value2",
  "foo" : "random_value"
}

But, the above JSON Schema fails to do so. It only checks whether "foo"/"bar" key or not, as per the value of "key_name". It fails to check for existence of any new key.

How to go about it?


回答1:


This was already answered here: Mutually exclusive property groups.

Additionally, you can find a great overview here: jsonSchema attribute conditionally required.


For your specific examples, the following approaches come to mind:

  1. Add "not": { "required": ["bar"] } to your first then clause to indicate that "bar" is not allowed. And the same for "foo" in the second then clause then.
  2. If there is always just "key_name" and one other property allowed, you could also simply add "maxProperties": 2 in the main schema.

EDIT (to address whitelisting alternative):

Another option would be to define each permutation separately like this:

{
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "key_name": { "const": "value1" },
        "foo": { "type": "string" }
      },
      "required": ["key_name", "foo"],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "key_name": { "const": "value2" },
        "bar": { "type": "string" }
      },
      "required": ["key_name", "bar"],
      "additionalProperties": false
    }
  ]
}


来源:https://stackoverflow.com/questions/61451539/is-there-a-way-to-raise-an-error-for-any-additional-key-present-in-json-if-i-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!