用于可变性的Json Schema模板

问题描述

我是刚定义JSON模式并根据该模式验证JSON的人。

这是一个示例json,我想为其定义一个JSON模式模板以进行验证:

{
 "version": "1.0", "config": {
   "globalConfig": {
      “ClientNames”: [
        “client1”,“client2”,“client3”
       ]
    },“ClientConfigs”: [
       {
          “ClientName”: “client1”,“property1”: “some value”,“property2”: “some value”
       },{
          “ClientName”: “client2”,{
          “ClientName”: “client3”,“property2”: “some value”
       }
    ]
}

据我了解,“ ClientConfigs”是一个对象数组(例如ClientConfig),其中将包含clientName,property1和property2。这是我认为架构想要的:

{
  "$schema": "http://json-schema.org/draft-01/schema#","title": "ClientConfig","type": "object","description": "Some configuration","properties": {
    "version": {
      "type": "string"
    },"config": {
      "$ref": "#/deFinitions/config"
    }
  },"deFinitions": {
    "config": {
      "type": "object","properties": {
        "globalConfig": {
          "type": "object","description": "Global config for all clients","properties": {
            "ClientNames": {
              "type": "array","minItems": 1,"items": {
                "type": "string"
              }
            }
          }
        },"ClientConfigs": {
          "type": "array","description": "List of configs for different clients","items": {
            "$ref": "#/deFinitions/ClientConfig"
          }
        }
      }
    },"ClientConfig": {
      "type": "object","properties": {
        "ClientName": {
          "type": "string"
        },"property1": {
          "type": "string"
        },"property2": {
          "type": "string"
        }
      }
    }
  }
}

我想用jsonschema验证两件事:

  1. ClientConfigs数组的每个元素中的ClientName是“ ClientNames”中的值之一,即“ ClientConfigs”数组中的各个ClientConfig应该只包含在属性“ ClientNames”中定义的客户端名称
  2. “ ClientNames”中存在的每个clientName应该定义为“ ClientConfigs”数组中的一个元素。更准确地说,为“ ClientNames”属性中存在的每个clientName定义ClientConfig

这是一个根据我的要求无效的示例:

{
 "version": "1.0",“ClientConfigs”: [
       {
          “ClientName”: “client4”,“property2”: “some value”
       }
    ]
}

这是无效的,因为:

  1. 它没有为client1,client2和client3定义ClientConfig
  2. 它为client4定义ClientConfig,它不在“ ClientNames”中。

是否可以使用json模式模板进行此类验证?如果是,该如何验证?

解决方法

您无法在JSON模式中引用实例数据。这被认为是业务逻辑,不在JSON模式的范围之内。