【问题标题】:JSON schema allOfJSON 模式 allOf
【发布时间】:2022-06-11 15:15:15
【问题描述】:

我有一个 json 模式在 allOf 条件下工作正常。但是,我不得不更改模式的结构,并且元素不再像以前那样在同一个地方,现在我似乎无法弄清楚为什么我得到一个有效的模式,而实际上它应该是无效的.所以示例模式是(为清楚起见而缩短,但在 allOf 中会有其他条件):

{
  "$ref": "#/$defs/Schema",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$defs": {
    "Schema": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Transaction": {
          "$ref": "#/$defs/Transaction"
        }
      }
    },
    "Transaction": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Header": {
          "$ref": "#/$defs/Header"
        },
        "Offer": {
          "$ref": "#/$defs/Offer"
        }
      },
      "required": ["Header"],
      "title": "Transaction"
    },
    "Header": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Flow": {
          "$ref": "#/$defs/Flow"
        }
      },
      "allOf": [
        {
          "$ref": "#/$defs/OFFER"
        }
      ]
    },
    "Offer": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Offer-Status": {
          "$ref": "#/$defs/Offer-Status"
        }
      }
    },
    "OFFER": {
      "if": {
        "type": "object",
        "properties": {
          "Transaction": {
            "type": "object",
            "properties": {
              "Header": {
                "type": "object",
                "properties": {
                  "Flow": {
                    "enum": ["Offer"]
                  }
                },
                "required": ["Flow"]
              }
            },
            "required": ["Header"]
          }
        },
        "required": ["Transaction"]
      },
      "then": {
        "type": "object",
        "properties": {
          "Transaction": {
            "type": "object",
            "properties": {
              "Offer": {
                "type": "object",
                "properties": {
                  "Offer-Status": {
                    "enum": ["NEW"]
                  }
                },
                "required": ["Offer-Status"]
              }
            },
            "required": ["Offer"]
          }
        },
        "required": ["Transaction"]
      }
    },
    "Flow": {
      "type": "string",
      "enum": ["Offer", "Acceptance"]
    },
    "Offer-Status": {
      "type": "string",
      "enum": ["NEW", ""]
    }
  }
}

所以本质上,条件应该是,如果 Header 中的 Flow 元素设置为“Offer”,则 Offer-Status 应该设置为 required 和“NEW”。

我尝试了许多不同的结构变体,但都没有成功验证。

输入的 JSON,应该是无效的(但显示为有效)是:

{
  "Transaction": {
    "Header": {
      "Flow": "Offer"
    },
    "Offer": {
      // "Offer-Status": "NEW"
    }
  }
}

可能是我没有看到的一个小问题,希望有人能看到这个问题。非常感谢。

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    您的所有架构都在定义中,但您实际上并未引用它们。所以“主”模式什么都不做,因此一切都是有效的。

    尝试将“架构”定义从 $defs 移到主体中(只是将其缩进两级)。

    【讨论】:

    • 谢谢@Ether 我已经尝试了以下但仍然没有快乐(假设这是你的意思)? { "$schema": "json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "Transaction": { "$ref": "#/$defs/Transaction" } }, "$defs": { "交易": {
    【解决方案2】:

    简化以显示可能解决方案的一般方法。添加其他架构部分应该很简单。

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
      "definitions": {
        "OFFERREQ": {
          "if": {
            "type": "object",
            "properties": {
              "Header": {
                "type": "object",
                "properties": {
                  "Flow": {
                    "enum": [ "Offer" ]
                  }
                },
                "required": [ "Flow" ]
              }
            },
            "required": [ "Header" ]
          },
          "then": {
            "type": "object",
            "properties": {
              "Offer": {
                "type": "object",
                "properties": {
                  "Offer-Status": {
                    "enum": [ "NEW" ]
                  }
                },
                "required": [ "Offer-Status" ]
              }
            },
            "required": [ "Offer" ]
          }
        }
      },
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Transaction": {
          "$ref": "#/definitions/OFFERREQ"
        }
      },
      "required": [ "Transaction" ]
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2020-02-03
      • 2017-06-02
      • 1970-01-01
      • 2016-12-08
      • 2022-01-14
      • 2019-02-21
      • 1970-01-01
      • 2021-03-02
      相关资源
      最近更新 更多