YAML 对列表的 JSON 架构:标签和嵌套的无标签字符串列表

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

我想为与下面的示例匹配的记录组合一个 JSON 模式验证器:

record_id: 3280
record_type: my_record_type
record_render_rules:
  - 3280_my_silly_object:
      - 3280_my_silly_predicate                                                                              

我对占用

3280_my_silly_object
3280_my_silly_predicate
的数据设置了单独的正则表达式模式限制。

以下是我迄今为止为渲染规则部分提出的内容。

"record_render_rules": {
  "type": "array",
  "items": {
    "type": "object",
    "patternProperties": {
      ".*": {
        "type": "array",
        "items": {
          "type": "string",
          "pattern": "my pattern"
        }
      }
    }
  }   
},

但是,我发现这并不令人满意,因为它对两种类型的数据应用了通用模式。 如上所述,我希望每种类型的数据都有一个单独的模式。

谢谢你们这些天才!

yaml jsonschema
1个回答
0
投票

您使用正确的语法是完全正确的,但您需要添加正则表达式来验证命名并限制

items
不允许除
patternProperties
定义之外的其他属性

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "record_id": {
            "type": "number"
        },
        "record_type": {
            "type": "string"
        },
        "record_render_rules": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": false,
                "patternProperties": {
                    "^[0-9]+_[a-z]+_[a-z]+_[a-z]+$": {
                        "type": "array",
                        "items": {
                            "type": "string",
                            "pattern": "^[0-9]+_[a-z]+_[a-z]+_[a-z]+$"
                        }
                    }
                }
            }
        }
    }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.