如何为 Map定义 Json 模式验证器

发布于 2025-01-09 14:24:45 字数 1021 浏览 0 评论 0原文

有人可以帮我定义以下 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

WorkflowConfig 具有 WorkflowNameNumberOfWorkersMap。其中WorkflowNameNumberOfWorkers是必填的,Map是可选的。

ActivityConfig 需要 ActivityNameMaxTime

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

提前致谢。

Can someone please help me defining Json Schema validator for the below Json.

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

Basically, above Json is a Map<String, WorkflowConfig>.

WorkflowConfig has WorkflowName, NumberOfWorkers and Map<String, ActivityConfigs>. Among these WorkflowName and NumberOfWorkers are mandatory, and Map<String, ActivityConfigs> is optional.

ActivityConfig has ActivityName and MaxTime both required.

I want to define Json schema for the above structure for schemaType: http://json-schema.org/draft-04/schema#.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2025-01-16 14:24:45

您可以使用以下架构。您也可以在线尝试 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 未固定,它可能是任何内容。

You can use the below schema. And you can also try it in online json
schema validator

{
    "$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"
                ]
            }
        }
    }
}

Here I'm using additionalProperties because Map<String, WorkflowConfig> in this key name is not fixed it may be anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文