如何组合 属性 类型,以匹配另一个属性类型

How to combine a property type, to match another properties type

我有以下使用 JSON 架构的用例。我有一个设置的元数据对象。在我们的例子中,设置可以是 string/real/integer/boolean.

类型

在这个对象中我有 4 个字段:default/minimum/maximum 每个定义一个 属性 的设置。 现在我想要实现的是,当默认值的类型是整数时,minimum/maximum 值也是整数。

到目前为止我想出的模式:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "setting-value": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        }
      ]
    },
    "setting-meta": {
      "type": "object",
      "required": [
        "name",
        "type",
        "default"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "Real",
            "Integer",
            "Boolean",
            "String"
          ]
        },
        "minimum": {
          "$ref": "#/definitions/setting-value"
        },
        "maximum": {
          "$ref": "#/definitions/setting-value"
        },
        "default": {
          "$ref": "#/definitions/setting-value"
        },
        "value": {
          "$ref": "#/definitions/setting-value"
        }
      }
    }
  }
}

#/definitions/setting-meta 可以支持不同的类型。但是,它没有定义如果例如 TYPE 的值等于 Real/Integer,则 minimum/maximum/default/value 的类型应该都是数字类型。

我会按如下方式使用这些定义

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "schema-definition-above.json#/definitions/setting-meta"
}

根据当前架构,以下所有示例均有效,但它们应该是 valid/invalid 所建议的:

有效JSon对象:

{
    "name": "Enabled",
    "type": "Boolean",
    "minimum": false,
    "maximum": true,
    "default": true,
    "value": true
}

无效的json对象,minimum/maximum/默认没有相同的类型:

{
    "name": "Enabled",
    "type": "Boolean",
    "minimum": false,
    "maximum": 1,
    "default": "value",
    "value": true
}

无效json对象:类型,与值的实际类型不匹配

{
    "name": "Enabled",
    "unit": "enabled/disabled",
    "configId": "Accumulator",
    "displayName": "Enable or disable this machine",
    "type": "Integer",
    "minimum": false,
    "maximum": true,
    "default": true,
    "value": true
}

我的问题: 是否可以将这些类型的依赖项放入 JSON 模式中?到目前为止,我发现的唯一一种依赖关系是 属性 依赖关系,表示如果设置了一个 属性,则还应该设置另一个。

如有任何帮助,我们将不胜感激。

编辑: 使用一些 JSON 对象扩展了该用例,这些对象应该使用引用的模式进行验证或失效。

为了在已知的一组可能条件下进行条件验证,您应该结合使用 if/then/else 关键字和 allOf.

在此架构中,allOf 中的第一个架构定义了您的总体结构和总体要求。如果 if 模式验证成功,第二个模式应用 then 约束。

您需要为每个条件复制第二个模式。

您可以在 https://jsonschema.dev 看到此架构(link 预加载了以下架构和示例数据)

(使用 patternProperties 只是一个 space 的节省。您可以单独定义每个 属性。)

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "properties": {
        "type": {
          "enum": [
            "Real",
            "Integer",
            "Boolean",
            "String"
          ]
        }
      },
      "required": [
        "type",
        "default",
        "name"
      ]
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "String"
          }
        }
      },
      "then": {
        "patternProperties": {
          "^(minimum|maximum|default|value)$": {
            "type": [
              "string"
            ]
          }
        }
      }
    }
  ]
}