Properties jsonschema要求和/或有条件要求的属性验证

Properties jsonschema要求和/或有条件要求的属性验证,properties,conditional,optional,jsonschema,required,Properties,Conditional,Optional,Jsonschema,Required,我需要验证始终具有2个属性的json对象: 类型 名字 类型可以是“A”、“B”或“C” 当类型为“A”时,还需要属性“foo”,不允许使用其他属性 好: 不正常: { "type": "A", "name": "a", "foo": "a", "lol": "a" } { "type": "C", "name": "a", "bar": "a", "lol": "a" } 当类型为“B”时,属性“bar”是必需的,不允许

我需要验证始终具有2个属性的json对象:

  • 类型
  • 名字
类型可以是“A”、“B”或“C”

当类型为“A”时,还需要属性“foo”,不允许使用其他属性

好:

不正常:

{
    "type": "A",
    "name": "a",
    "foo": "a",
    "lol": "a"
}
{
    "type": "C",
    "name": "a",
    "bar": "a",
    "lol": "a" 
}
当类型为“B”时,属性“bar”是必需的,不允许有其他属性

当类型为“C”时,属性“bar”是必需的,也可以选择显示“zen”属性

好:

不正常:

{
    "type": "A",
    "name": "a",
    "foo": "a",
    "lol": "a"
}
{
    "type": "C",
    "name": "a",
    "bar": "a",
    "lol": "a" 
}
不幸的是,这个问题的突出答案部分涵盖了我的情况,但是我没有成功构建一个适合我的jsonschema

编辑:

这是我试过的

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "properties": {
        "type": {
            "type": "string",
            "enum": ["A", "B", "C"]
        },
        "name": {"type": "string"},
        "foo": {"type": "string"},
        "bar": {"type": "string"},
        "zen": {"type": "string"},
    },
    "anyOf": [
        {
            "properties": {"type": {"enum": ["A"]}},
            "required": ["foo"],
        },
        {
            "properties": {"type": {"enum": ["B"]}},
            "required": ["bar"],
        },
        {
            "properties": {"type": {"enum": ["C"]}},
            "required": ["bar"],
        },
    ]
}
我的问题是,在“anyOf”中的对象中,将字段“additionalProperties”设置为false不会得到预期的结果

例如,下面的json通过了验证,尽管它有额外的属性“lol”


JSON模式是一个约束系统,其中每个子模式的约束分别进行评估。这意味着“additionalProperties”只能“查看”同一立即模式对象中的“属性”或“模式属性”

此外,它不能基于“必需”来“查看”属性,只能基于“属性”和“模式属性”


据我所知,如果您在anyOf的每个分支内设置“additionalProperties”:false,则所有这些都不应该起作用,因为唯一允许的属性是“type”。如果您这样做了,并且它允许“type”以外的属性,那么我想知道您使用的是什么实现。

下面的模式在您的示例中适用于我。希望这能帮助其他人。诀窍是结合使用
附加属性
最大属性
,在正确的位置使用
必需的

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "A",
        "B",
        "C"
      ]
    },
    "name": {
      "type": "string"
    },
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    },
    "zen": {
      "type": "string"
    }
  },
  "required": [
    "name",
    "type"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "A"
          }
        }
      },
      "then": {
        "required": [
          "foo"
        ],
        "maxProperties": 3
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "B"
          }
        }
      },
      "then": {
        "required": [
          "bar"
        ],
        "maxProperties": 3
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "C"
          }
        }
      },
      "then": {
        "required": [
          "bar"
        ],
        "maxProperties": 4
      }
    }
  ],
  "additionalProperties": false
}

我更新了我的帖子,加入了我尝试过的示例,以及该解决方案不适用于meso的原因。基本上,实现我需要的刚性模式的唯一方法是使用anyOf和模式列表来验证我需要的内容,我想重用一些属性,但这似乎是不可能的。在草稿-06中,您可以对
propertyNames
进行一些重复使用,尽管它并不十分简洁。
{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "A",
        "B",
        "C"
      ]
    },
    "name": {
      "type": "string"
    },
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    },
    "zen": {
      "type": "string"
    }
  },
  "required": [
    "name",
    "type"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "A"
          }
        }
      },
      "then": {
        "required": [
          "foo"
        ],
        "maxProperties": 3
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "B"
          }
        }
      },
      "then": {
        "required": [
          "bar"
        ],
        "maxProperties": 3
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "C"
          }
        }
      },
      "then": {
        "required": [
          "bar"
        ],
        "maxProperties": 4
      }
    }
  ],
  "additionalProperties": false
}