JSONSchema基于Enum值验证JSON

qkf9rpyu  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(116)

我有一个JSON,需要创建JSONSchema来验证它。
用例:在选择服务“Glue”或“S3”时,它应该验证一个或多个具有action作为前缀“/glue”或“/s3”的资源。它还验证正确的输入资源及其属性。在请求JSON中可以有一个或多个资源。

{
    "service": [
        "Glue",
        "S3"
    ],
    "resources": [
        {
            "action": "/glue/list",
            "tags": [
                "glueall"
            ]
        },
        {
            "action": "/glue/get/db",
            "tags": [
                "gluedb"
            ],
            "databaseName": "example"
        },
        {
            "action": "/s3/list",
            "tags": [
                "s3all"
            ]
        },
        {
            "action": "/s3/get/bucket",
            "tags": [
                "s3bucket"
            ],
            "bucketName": "example"
        }
    ]
}
f0brbegy

f0brbegy1#

这仅涵盖两个用例:
这不是基于主题行中请求的枚举值。如果你需要,这可以相应地修改。

  • 如果action/glue开始,以db结束,则dataBaseNamerequired
  • 如果action/s3开始并以bucket结束,则bucketNamerequired
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["service", "resources"],
    "additionalProperties": false,
    "properties": {
        "service": {
            "type": "array",
            "uniqueItems": true,
            "minItems": 1,
            "items": {
                "type": "string"
            }
        },
        "resources": {
            "type": "array",
            "uniqueItems": true,
            "minItems": 1,
            "items": {
                "type": "object",
                "required": ["action", "tags"],
                "properties": {
                    "action": {
                        "type": "string",
                        "pattern": "^\\/"
                    },
                    "tags": {
                        "type": "array",
                        "uniqueItems": true,
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "allOf": [
                    {
                        "if": {
                            "properties": {
                                "action": {
                                    "pattern": "^\\/s3.*\\/bucket$"
                                }
                            }
                        },
                        "then": {
                            "required": [
                                "bucketName"
                            ]
                        }
                    },
                    {
                        "if": {
                            "properties": {
                                "action": {
                                    "pattern": "^\\/glue.*\\/db$"
                                }
                            }
                        },
                        "then": {
                            "required": [
                                "databaseName"
                            ]
                        }
                    }
                ]
            }
        }
    }
}

相关问题