【问题标题】:jsonschema validation of properties required and/or conditionally required需要和/或有条件需要的属性的 jsonschema 验证
【发布时间】:2018-03-20 19:37:24
【问题描述】:

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

  • 类型
  • 姓名

类型可以是“A”、“B”或“C”,

当 type 为 "A" 时,属性 "foo" 也是必需的,不允许有其他属性。

好的:

{
    "type": "A",
    "name": "a",
    "foo": "a",
}

不行:

{
    "type": "A",
    "name": "a",
    "foo": "a",
    "lol": "a"
}

当 type 为“B”时,属性“bar”是必需的,不允许有额外的属性。

当类型为“C”时,属性“bar”是必需的,并且可选地也可以存在“zen”属性。

好的:

{
    "type": "C",
    "name": "a",
    "bar": "a",
    "zen": "a"
}

{
    "type": "C",
    "name": "a",
    "bar": "a",
}

不行:

{
    "type": "C",
    "name": "a",
    "bar": "a",
    "lol": "a" 
}

不幸的是,这个question 的出色答案部分涵盖了我的情况,但是我没有设法构建一个适合我的 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”

{
    "type": "A",
    "name": "a",
    "foo": "a",
    "lol": "a"
}

【问题讨论】:

  • 我更新了我的帖子以包含我尝试过的示例以及此解决方案对我不起作用的原因

标签: properties conditional optional jsonschema required


【解决方案1】:

以下架构在您的示例中对我有用。希望这对其他人有所帮助。诀窍是使用additionalPropertiesmaxProperties 的组合,并在正确的位置使用required

{
  "$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
}

【讨论】:

    【解决方案2】:

    JSON Schema 是一个约束系统,其中每个子模式的约束都是单独评估的。这意味着“additionalProperties”只能“看到”同一个直接模式对象内的“属性”或“模式属性”。

    此外,它不能“看到”基于“required”的属性,只能基于“properties”和“patternProperties”。

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

    【讨论】:

    • 所以基本上实现我需要的刚性模式的唯一方法是将 anyOf 与验证我需要的模式列表一起使用,我想重用一些属性,但似乎这是不可能的。
    • 在 Draft-06 中,您可以对 propertyNames 进行一些重用,尽管它不是非常简洁。
    猜你喜欢
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    相关资源
    最近更新 更多