元模式指定所有属性的必需属性 [英] Metaschema specifying required attribute for all properties

查看:54
本文介绍了元模式指定所有属性的必需属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义一个元模式,以便要求所有属性都具有一个附加属性,例如,我如何要求所有属性都指定一个"type"?

I want to customize a metaschema such that all properties are required to have an additional attribute, for example, how could I require that all properties specify a "type"?

然后此架构应失败:

{
    "$schema": "http://json-schema.org/schema#",
    "title": "...",
    "description": "...",
    "type": "object",
    "properties": {
        "name": {
            "description": "..."
        }
    }
}

但是这个应该成功:

{
    "$schema": "http://json-schema.org/schema#",
    "title": "...",
    "description": "...",
    "type": "object",
    "properties": {
        "name": {
            "description": "...",
            "type": "string"
        }
    }
}

推荐答案

不幸的是,编写元方案并不容易.它正在开发中,但是还没有好的解决方案.

Unfortunately, writing meta-schemas is not easy. It's being worked on, but there's no good solution yet.

您必须复制要扩展的元模式,然后添加"required": ["type"].

You would have to make a copy of the meta-schema you want to extend and then add "required": ["type"].

但是,当我们在这里时,也许我可以说服您不要这样做.在某些情况下,使type关键字成为必需的会导致问题.这里有一些例子. https://github.com/json-schema/json- schema/issues/172#issuecomment-124214534

But, while we're here, maybe I can convince you not to do this. Making the type keyword required causes problems in some cases. Here are a few examples. https://github.com/json-schema/json-schema/issues/172#issuecomment-124214534

在进一步讨论之后,我们发现这种特殊情况不存在我们通常在扩展元模式时遇到的问题,因为它不需要递归.这是一个扩展拔模06架构以包括名为custom的新关键字的示例,该关键字是布尔值,仅在properties架构的顶层才需要.

After discussing this further, we found that this particular case doesn't have the problems we normally run into with extending meta-schemas because it doesn't need to be recursive. Here is an example of extending the draft-06 schema to include a new keyword called custom which is a boolean and is required only at the top level of a properties schema.

{
  "allOf": [
    { "$ref": "http://json-schema.org/draft-06/schema#" },
    {
      "properties": {
        "properties": {
          "patternProperties": {
            ".*": {
              "properties": {
                "custom": { "type": "boolean" }
              },
              "required": ["custom"]
            }
          }
        }
      }
    }
  ]
}

这是符合此元模式的示例架构.

And here's an example schema that conforms to this meta-schema.

{
  "properties": {
    "foo": {
      "custom": true,
      "not": { "type": "string" }
    }
  }
}

对于custom关键字是必需的.模式,但不是not模式或顶级模式.

The custom keyword is required for the "foo" schema, but not the not schema or the top level schema.

这篇关于元模式指定所有属性的必需属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆