英文:
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 是一个对象,并且它还有另一个对象 lineId,productId 只能包含字符串或数字字段,而不能是一个对象,
如何扩展元模式以强制执行此要求。
非常感谢任何帮助。
英文:
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版本,操作会更加复杂。
- 复制draft-04元模式。
 - 删除任何在子模式中不希望出现的内容,包括“object”类型和与对象相关的任何关键词,比如“properties”。
 - 将“id”更改为唯一的值,例如“https://my-project.com/nested-meta-schema”。
 - 再次复制draft-04元模式。
 - 将所有递归引用(
{ "$ref": "#" })替换为对刚刚创建的模式的引用({ "$ref": "https://my-project.com/nested-meta-schema" })。 - 将第二个模式的“id”更改为唯一的值,例如“https://my-project.com/flat-meta-schema”。
 - 对于任何要根据你的元元模式进行验证的模式,将"$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.
- Make a copy of the draft-04 meta schema
 - Remove anything you don't want to be allowed in sub-schemas including the "object" 
typeand any keywords related to objects such asproperties. - Change the 
idto something unique likehttps://my-project.com/nested-meta-schema. - Make another copy of the draft-04 meta schema
 - Replace all recursive references (
{ "$ref": "#" }) with references to the schema you just made ({ "$ref": "https://my-project.com/nested-meta-schema" }) - Change the 
idof the second schema to something unique likehttps://my-project.com/flat-meta-schema. - For any schema you want to validate against your meta-schema, change the 
$schemato the id you gave the second meta-schema. 
Note that not all implementations support custom meta-schemas, so your mileage may vary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论