【问题标题】:Validate JSON Schema with another JSON Schema使用另一个 JSON Schema 验证 JSON Schema
【发布时间】:2021-04-18 08:01:13
【问题描述】:

我正在尝试使用另一个 JSON 架构来验证 JSON 架构。

要验证的 JSON Schema 示例:https://jsonschema.net/home

用于验证上述架构的验证架构参考:https://github.com/ajv-validator/ajv/blob/master/lib/refs/json-schema-draft-07.json

我有一个要求,property 只能是原始类型,即string, number, integer, boolean。 由于 JSON 模式的根应该有 type 作为 object 并且它里面的所有 properties 都将有 type 作为原始类型,我不知道我应该如何定义 type 定义来验证 type在根级别作为object 而type 在properties 内部作为原始类型。

示例 JSON:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "name": "A green door"
        }
    ],
    "required": [
        "name"
    ],
    "properties": {
        "name": {
            "$id": "#/properties/name",
            "type": "string",
            "title": "The name schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "A green door"
            ]
        }
    },
    "additionalProperties": true
}

验证type的验证JSON:

  definitions: {
    simpleTypes: {
      enum: ['array', 'boolean', 'integer', 'null', 'number', 'object', 'string'],
    }
  },
  properties: {
  type: {
      anyOf: [
        { $ref: '#/definitions/simpleTypes' },
      ],
    },
  }

从上面的simpleTypes -> enum,如果我删除object,我的JSON 就会失效。

我可以以任何方式为根 type 定义 enum 与 properties 中的 type 不同吗?

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    首先复制 draft-07 架构并为其指定一个唯一的$id。

    {
      "$schema": "http://json-schema.org/draft-07/schema",
      "$id": "https://example.com/my-custom-draft-07-schema",
      "definitions": {
        ...original-definitions...
      },
      ...original-schema...
    }
    

    由于您希望在不同位置对架构有不同的约束,因此您需要将架构重构为一个定义,以便在不同情况下对其进行引用和扩展。不要忘记将所有递归引用 ({ "$ref": "#" }) 更改为指向您创建的定义 ({ "$ref": "#/definitions/schema" })。

    {
      "$schema": "http://json-schema.org/draft-07/schema",
      "$id": "https://example.com/my-custom-draft-07-schema",
      "alllOf": [{ "$ref": "#/definitions/schema" }],
      "definitions": {
        ...original-definitions-with-modified-$refs...
       "schema": {
          ...original-schema-with-modified-$refs...
        }
      }
    }
    

    接下来,您需要添加另一个特定于属性架构的定义,并更改属性架构的 $refs 以使用创建的定义 ({ "$ref": "#/definitions/property-schema" })。不要忘记更改patternProperties 和additionalProperties 以及properties。其他关键字,例如 anyOf 应继续引用通用架构 ({ "$ref": "#/definitions/schema" })。

    {
      "$schema": "http://json-schema.org/draft-07/schema",
      "$id": "https://example.com/my-custom-draft-07-schema",
      "alllOf": [{ "$ref": "#/definitions/schema" }],
      "definitions": {
        ...original-definitions-with-modified-$refs...
        "schema": {
          ...original-schema-with-modified-$refs...
        },
        "property-schema": {
          "allOf": [{ "$ref": "#/definitions/schema" }]
        }
      }
    }
    

    此时,架构刚刚被重构。除了您现在有一个放置自定义约束的地方之外,所有行为都没有改变,因此它们只会应用到您想要它们的位置。从这里添加约束应该相对简单。

    {
      "$schema": "http://json-schema.org/draft-07/schema",
      "$id": "https://example.com/my-custom-draft-07-schema",
      "alllOf": [{ "$ref": "#/definitions/schema" }],
      ...add-root-schema-constraints-here...
      "definitions": {
        ...original-definitions-with-modified-$refs...
        "schema": {
          ...original-schema-with-modified-$refs...
          ...add-global-constraints-here...
        },
        "property-schema": {
          "allOf": [{ "$ref": "#/definitions/schema" }]
          ...add-property-schema-constraints-here...
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多