两组patternProperties中的任何一个,但没有附加属性

问题描述 投票:0回答:1

我正在尝试使用

patternProperties
定义两组可能的
anyOf
,但我想排除任何不符合这些模式的属性。但是,在两组
additionalProperties: false
之后定义
patternProperties
时,我收到一个错误,没有任何属性匹配。

这是一些示例 JSON 数据:

{
  "fruitbasket": {
    "apple": {
      "color": "green"
    },
    "pear": {
      "color": "green"
    },
    "banana": {
      "color": "yellow"
    },
    "apple_wrapper": {
      "material": "paper"
    },
    "pear_wrapper": {
      "material": "cloth"
    },
    "banana_wrapper": {
      "material": "none"
    }
  }
}

这是我的架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/responseobject",
  "definitions": {
    "responseobject": {
      "type": "object",
      "properties": {
        "fruitbasket": {
          "anyOf": [
            {
              "$ref": "#/definitions/fruit"
            },
            {
              "$ref": "#/definitions/wrapper"
            }
          ]
        }
      },
      "additionalProperties": false
    },
    "fruit": {
      "type": "object",
      "patternProperties": {
        "^(apple|pear|banana)$": {
          "type": "object",
          "properties": {
            "color": {
              "type": "string",
              "enum": [
                "green",
                "yellow",
                "red"
              ]
            }
          },
          "required": [
            "color"
          ],
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "wrapper": {
      "type": "object",
      "patternProperties": {
        "^(apple|pear|banana)_wrapper$": {
          "properties": {
            "material": {
              "type": "string",
              "enum": [
                "paper",
                "cloth",
                "none"
              ]
            }
          },
          "required": [
            "material"
          ],
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }
  }
}

如果我删除与两个

"additionalProperties": false
之一一起定义的
patternProperties
,架构就会验证。在当前的 JSON 模式中,这种严格程度是否可能?我应该如何只允许两组 patternProperties 而不允许其他属性?

这是显示错误的在线验证示例:https://www.jsonschemavalidator.net/s/DQqnlLBT

json jsonschema json-schema-validator
1个回答
0
投票

实际上我想我找到了答案。无需使用

anyOf
,只需将
patternProperties
一个接一个地放即可:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/responseobject",
  "definitions": {
    "responseobject": {
      "type": "object",
      "properties": {
        "fruitbasket": {
          "type": "object",
          "patternProperties": {
            "^(apple|pear|banana)$": {
              "$ref": "#/definitions/fruit"
            },
            "^(apple|pear|banana)_wrapper$": {
              "$ref": "#/definitions/wrapper"
            }
          },
          "additionalProperties": false
        }
      }
    },
    "fruit": {
      "type": "object",
      "properties": {
        "color": {
          "type": "string",
          "enum": [
            "green",
            "yellow",
            "red"
          ]
        }
      },
      "required": [
        "color"
      ],
      "additionalProperties": false
    },
    "wrapper": {
      "properties": {
        "material": {
          "type": "string",
          "enum": [
            "paper",
            "cloth",
            "none"
          ]
        }
      },
      "required": [
        "material"
      ],
      "additionalProperties": false
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.