【问题标题】:Json Meta Schema : How to restrict another Json Schema from having nested objectsJson Meta Schema:如何限制另一个 Json Schema 嵌套对象
【发布时间】:2020-12-26 17:01:33
【问题描述】:

所以我正在使用 Json Meta Schema https://json-schema.org/draft/2019-09/meta/core 使用https://github.com/java-json-tools/json-schema-validator进一步验证 JSONSchema

我有一个要求,我必须限制架构包含嵌套对象,比如下面的架构应该是无效的

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/product.schema.json",
  "title": "test",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "Outer",
      "type": "object",
      "properties": {
        "lineId": {
          "description": "Outer",
          "type": "object"
        }
      }
    }
  }
}

由于 productId 是一个对象并且它有另一个对象 lineIdproductId 只能有字符串或数字字段,但不能有一个对象, 如何扩展 MetaSchema 以强制执行此操作。 任何帮助表示赞赏

【问题讨论】:

  • 您很少想要扩展元模式。您能否详细说明为什么您认为这是您想做的事情,以及您最初的目标是什么?这可能是 XY 类型的问题。
  • 另外我注意到你已经链接到draft 2019-09 元模式,而你的模式定义了你正在使用draft-07。你打算用哪个?
  • 另外,您使用的实现仅支持draft-03 和draft-04。

标签: java json jsonschema jsonpath json-schema-validator


【解决方案1】:

您的问题有一些版本不一致,但我假设是 draft-04,因为您使用的验证器仅支持 draft-04。如果您需要对其他草稿执行此操作,此过程将与 draft-07 类似。 2019-09 草案会更复杂。

  1. 复制draft-04 元模式
  2. 删除您不希望在子架构中允许的任何内容,包括“对象”type 以及与对象相关的任何关键字,例如 properties
  3. id 更改为独特的名称,例如https://my-project.com/nested-meta-schema
  4. 制作另一份 Draft-04 元模式的副本
  5. 将所有递归引用 ({ "$ref": "#" }) 替换为对您刚刚创建的架构的引用 ({ "$ref": "https://my-project.com/nested-meta-schema" })
  6. 将第二个架构的 id 更改为独特的名称,例如 https://my-project.com/flat-meta-schema
  7. 对于您要针对您的元架构进行验证的任何架构,请将 $schema 更改为您为第二个元架构提供的 ID。

请注意,并非所有实现都支持自定义元模式,因此您的情况可能会有所不同。

【讨论】:

    猜你喜欢
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多