如何在单个 json 架构文档中支持多个顶级命名空间?这是用于 helm 子图表的

问题描述 投票:0回答:1

我知道“命名空间”不是正确的术语,但我很难找到 json 模式文档或在线解释的这个用例。

我使用 json 模式作为 helm 子图表。无需深入了解 helm 细节,这意味着该模式将用于验证如下所示的 yaml 文档(在子图表自己的项目中):

# values.yaml in mysubchart
propOne:
 derp: 123
 herp: abc

还有类似这样的 yaml 文档(在父图表项目中):

# values.yaml in mysubchart
mysubchart:
    propOne:
     derp: 123
     herp: abc

所以我有两个文档想要使用相同的模式进行验证。唯一的区别是其中一个文档有一个顶级字段,所有内容都嵌套在 (

mysubchart:
)

是否可以支持具有相同 json 架构的两个文档? Helm 将使用 helm lint 验证架构,不会出现任何问题,但我也希望用户能够将他们的编辑器指向我发布的 json 架构,以验证父图表中的值 yaml 并获得自动完成功能等。

编辑

我尝试过此操作,但它无法验证任一 yaml 文档:

{
    "$defs": {
        "mychart": {
            "$schema": "http://json-schema.org/schema#",
            "title": "test",
            "description": "test",
            "type": "object",
            "required": [
                "propOne",
                "propTwo"
            ],
            "properties": {
                "propOne": {
                    "description": "sdkfjlsdfjlksdjf",
                    "type": "string"
                },
                "propTwo": {
                    "description": "sdkfjlsdfjlksdjf",
                    "type": "string"
                },
            }
        },
    },
    "oneOf": [
        {
            "$ref": "#/$defs/mychart"
        },
        {
            "properties": {
                "mychart": {
                    "$defs": "#/$defs/mychart"
                }
            }
        }
    ]
}

看起来这适用于验证我的第一个 yaml 文档:

"anyOf": [
    {
        "$ref": "#/$defs/mychart"
    }
] 

但是用

properties
添加第二个元素来尝试验证第二个文档却搞砸了,并且都没有正确验证

这有效,我缺少配置的必需:mysubchart 部分

kubernetes-helm jsonschema
1个回答
0
投票

您可以使用

oneOf
组合多个模式,并将公共元素移动到两个模式引用的定义中:

  $defs:
    chart:
      type: object
      required: [ propOne ]
      properties:
        propOne:
          required: [ derp, herp ]
          properties:
            derp:
              type: integer
            herp:
              type: string
  type: object
  oneOf:
  - $ref: '#/$defs/chart'
  - required: [ mysubchart ]
    properties:
      mysubchart:
        $defs: '#/$defs/chart'

如果您使用的实现仅支持 Draft7 或更早版本,请将

$defs
更改为
definitions

组合模式包含在 https://json-schema.org/understanding-json-schema/reference/combining.html

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.