Json Meta Schema: 如何限制另一个 Json Schema 具有嵌套对象

huangapple go评论78阅读模式
英文:

Json Meta Schema : How to restrict another Json Schema from having nested objects

问题

以下是翻译好的部分:

我正在使用一个 Json 元模式 https://json-schema.org/draft/2019-09/meta/core 来进一步验证 JSONSchema,使用 https://github.com/java-json-tools/json-schema-validator 进行验证。

我有一个要求,我必须限制模式不包含嵌套对象,就像下面的模式应该是无效的:

{
  "$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 只能包含字符串或数字字段,而不能是一个对象,
如何扩展元模式以强制执行此要求。
非常感谢任何帮助。

英文:

So I am using a Json Meta Schema https://json-schema.org/draft/2019-09/meta/core
to further validate JSONSchema using https://github.com/java-json-tools/json-schema-validator

I have a requirement where I have to restrict a schema from having nested objects , like the below schema should be invalid

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

Since productId is an object and it has another object lineId , productId can have only string or number fields but never an object ,
How to extend the MetaSchema to enforce this.
Any help is appreciated

答案1

得分: 2

你的问题存在一些版本不一致的情况,但我会假设你在使用的验证器只支持draft-04版本。如果你需要针对其他草案进行操作,这个步骤在draft-07版本之前是类似的。对于Draft 2019-09版本,操作会更加复杂。

  1. 复制draft-04元模式。
  2. 删除任何在子模式中不希望出现的内容,包括“object”类型和与对象相关的任何关键词,比如“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。

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

英文:

Your question has some version inconsistency, but I'll assume draft-04 since the validator you are using only supports up to draft-04. If you need to do this for other drafts, this procedure will be similar through draft-07. Draft 2019-09 would be more complicated.

  1. Make a copy of the draft-04 meta schema
  2. Remove anything you don't want to be allowed in sub-schemas including the "object" type and any keywords related to objects such as properties.
  3. Change the id to something unique like https://my-project.com/nested-meta-schema.
  4. Make another copy of the draft-04 meta schema
  5. Replace all recursive references ({ "$ref": "#" }) with references to the schema you just made ({ "$ref": "https://my-project.com/nested-meta-schema" })
  6. Change the id of the second schema to something unique like https://my-project.com/flat-meta-schema.
  7. For any schema you want to validate against your meta-schema, change the $schema to the id you gave the second meta-schema.

Note that not all implementations support custom meta-schemas, so your mileage may vary.

huangapple
  • 本文由 发表于 2020年9月8日 18:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63791703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定