【问题标题】:JSON Schema for objects without names没有名称的对象的 JSON 模式
【发布时间】:2020-10-31 18:20:20
【问题描述】:

我想编写验证对象列表的架构的一部分,其中列表中的对象没有名称:

"some list": [
    {
        "thing 1": "foo",
        "thing 2": 100
    },
    {
        "thing 1": "foo",
        "thing 2": 100
    },
    {
        "thing 1": "foo",
        "thing 2": 100
    },
]

我有一个工作模式,其中包含我想要删除的额外键名,标记为I WANT TO GET RID OF THIS NAME。我猜你可以认为它没有该对象的属性名称。

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "id": "v2",
    "properties": {
        "some list": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "I WANT TO GET RID OF THIS NAME": {
                        "type": "object",
                        "properties": {
                            "thing 1": {
                                "type": "string",
                                "description": "a string"
                            },
                            "thing 2": {
                                "type": "integer",
                                "minimum": 0,
                                "description": "Default time to expose a single image layer for."
                            }
                        },
                        "additionalProperties": false
                    },
                    "additionalProperties": false
                }
            }
        }
    }
}

我不能仅仅因为模式规范期望它而删除名称,但我也无法弄清楚如何告诉它那些对象没有名称。我正在使用 jsonschema Draft7Validator 使用 Python 3.7 运行此示例

【问题讨论】:

    标签: python json schema jsonschema


    【解决方案1】:

    删除该属性是正确的-它指的是错误的嵌套级别。架构应如下所示:

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "id": "v2",
      "properties": {
        "some list": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "thing 1": {
                "type": "string",
                "description": "a string"
              },
              "thing 2": {
                "type": "integer",
                "minimum": 0,
                "description": "Default time to expose a single image layer for."
              }
            },
            "additionalProperties": false
          }
        }
      }
    }
    

    【讨论】:

    • 我意识到我在查看此内容时实际上删除了错误的名称。我真的很想摆脱"thing 1""thing 2" 的名称。我会用它发布一个新问题,如果你知道如何解决它,去看看
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多