如何为 Map<String, Object> 定义 Json 模式验证器

How to define Json schema validator for Map<String, Object>

有人可以帮我定义 Json 下面 Json 的架构验证器吗?

{
  "WorkflowName1": {
    "WorkflowName": "WorkflowName1",
    "NumberOfWorkers": 1,
    "ActivityConfigs": {
      "ActivityName1": {
        "ActivityName": "ActivityName1",
        "MaxTime": 200
      }
    }
  },
  "WorkflowName2": {
    "WorkflowName": "WorkflowName2",
    "NumberOfWorkers": 10,
    "ActivityConfigs": {
      "ActivityName1": {
        "ActivityName": "ActivityName2",
        "MaxTime": 2000
      }
    }
  }
}

基本上Json上面就是一个Map<String, WorkflowConfig>.

WorkflowConfig 有 WorkflowNameNumberOfWorkersMap<String, ActivityConfigs>。其中WorkflowNameNumberOfWorkers是必须的,Map<String, ActivityConfigs>是可选的。

ActivityConfig 需要 ActivityNameMaxTime

我想为 schemaType 的上述结构定义 Json 模式:http://json-schema.org/draft-04/schema#

提前致谢。

您可以使用以下架构。您也可以在线尝试 json 模式验证器

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": {
        "type": "object",
        "properties": {
            "WorkflowName": {
                "type": "string"
            },
            "NumberOfWorkers": {
                "type": "number"
            },
            "ActivityConfigs": {
                "$ref": "#/definitions/configDef"
            }
        },
        "required": [
            "WorkflowName",
            "NumberOfWorkers"
        ]
    },
    "definitions": {
        "configDef": {
            "type": "object",
            "additionalProperties": {
                "type": "object",
                "properties": {
                    "ActivityName": {
                        "type": "string"
                    },
                    "MaxTime": {
                        "type": "number"
                    }
                },
                "required": [
                    "ActivityName",
                    "MaxTime"
                ]
            }
        }
    }
}

这里我使用的是additionalProperties,因为这个键名中的Map<String, WorkflowConfig>是不固定的,它可能是任何东西。