【问题标题】:Json Schema for a json schema with complex conditions具有复杂条件的 json 模式的 Json Schema
【发布时间】:2020-09-29 14:20:08
【问题描述】:

我需要验证给定的 JSON 模式实例是否遵循某些规则。 大多数规则都可以根据其他模式进行检查,但我无法使用模式检查 2 个条件:

  1. 对于每个“对象”类型,“属性”中的每个属性也必须在“必需”列表中。
  2. '$schema' 必须且只能位于架构的根目录中。

事实上,我已经通过在 python 脚本中创建 custom validators 成功验证了这些条件,但我想知道,
是否可以纯粹使用 JSON 模式来检查这些条件?

如果您感兴趣,您可以看到我正在使用的当前架构,尽管我的问题没有必要:

{
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "types": {
            "object": {
                "_comment": "object type",
                "isType": "object",
                "properties": {
                    "type": {"enum": ["object"]},
                    "properties": {
                        "type": "object",
                        "additionalProperties": {"$ref": "#/definitions/schema"},
                    },
                    "additionalProperties": {"enum": [False]},
                    "required": {"type": "array"},
                    "description": {"type": "string", "maxLength": 128},
                    "$id": {"type": "string", "maxLength": 128}

                },
                "dependencies": {
                    "properties": ["required"],
                    "required": ["properties"]
                },
                "required": ["additionalProperties"],
                "allRequired": True,  # custom validator for requiring all properties
                "additionalProperties": False
            },
         ... # extra types that not really relevant
        },

        "schema": {
            "oneOf": [
                {"$ref": "#/definitions/types/object"},
                {"$ref": "#/definitions/types/string"},
                {"$ref": "#/definitions/types/number"},
                {"$ref": "#/definitions/types/array"},
                {"$ref": "#/definitions/types/enum"},
                {"$ref": "#/definitions/types/anyOf"},
            ],

        },
    },

    "type": "object",
    "$ref": "#/definitions/schema"
}


如果您不知道是否可行,也许您可​​以帮助我解决以下问题之一:

  1. 是否可以引用当前正在验证的实例(类似于 '$ref':"#" 引用当前架构)
  2. 是否有可能以某种方式使用“dependencies”验证器来验证条件 1?

【问题讨论】:

    标签: python json jsonschema


    【解决方案1】:
    1. 对于每个“对象”类型,“属性”中的每个属性也必须在“必需”列表中。

    做不到,因为……

    1. 是否可以引用当前正在验证的实例(类似于 '$ref':"#" 引用当前架构)

    没有。有人提议使用类似 $data 的关键字,但这个概念存在太多问题,因此从未实现。

    1. 是否有可能以某种方式使用“依赖项”验证器来验证条件 1?

    没有。

    1. '$schema' 必须且只能位于架构的根目录中。

    这个你可以做到。基本上,您可以创建定义了所有内容的“模式”定义。然后您可以将“模式”扩展为在根模式中要求 $schema 或在子模式中禁止 $schema

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
    
      "allOf": [{ "$ref": "#/definitions/schema" }],
      "required": ["$schema"],
    
      "definitions": {
        "schema": {
          "type": "object",
          "properties": {
            "$id": { "type": "string", "format": "uri" },
            "$schema": { "type": "string", "format": "uri" },
            "properties": {
              "type": "object",
              "additionalProperties": { "$ref": "#/definitions/sub-schema" }
            }
          },
          "additionalProperties": false
        },
        "sub-schema": {
          "allOf": [{ "$ref": "#/definitions/schema" }],
          "not": { "required": ["$schema"] }
        }
      }
    }
    

    【讨论】:

    • 感谢详细解答的朋友!
    【解决方案2】:

    不,这对于 JSON Schema 是不可能的。您不能引用实例数据。

    【讨论】:

    • 好的,谢谢,那么仅在 root 上要求 $schema 怎么样?
    • 实际上可能。我会玩一会然后回复你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2019-10-18
    相关资源
    最近更新 更多