创建JSONSchema验证关联Glue/S3服务的请求JSON资源合法性
JSON Schema 验证规则实现
以下是满足需求的 JSON Schema,可用于验证指定格式的请求数据:
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["service", "resources"], "properties": { "service": { "type": "array", "minItems": 1, "items": { "type": "string", "enum": ["Glue", "S3"] }, "uniqueItems": true }, "resources": { "type": "array", "minItems": 1, "items": { "type": "object", "required": ["action", "tags"], "properties": { "action": { "type": "string", "pattern": "^/(glue|s3)/.*", "allOf": [ { "$comment": "校验/glue开头的action对应服务是否被选中", "if": { "properties": { "action": { "pattern": "^/glue/.*" } } }, "then": { "properties": { "service": { "contains": { "const": "Glue" } } } } }, { "$comment": "校验/s3开头的action对应服务是否被选中", "if": { "properties": { "action": { "pattern": "^/s3/.*" } } }, "then": { "properties": { "service": { "contains": { "const": "S3" } } } } } ] }, "tags": { "type": "array", "minItems": 1, "items": { "type": "string" } }, "databaseName": { "type": "string" }, "bucketName": { "type": "string" } }, "$comment": "根据action类型校验必填属性", "allOf": [ { "if": { "properties": { "action": { "const": "/glue/get/db" } } }, "then": { "required": ["databaseName"] } }, { "if": { "properties": { "action": { "const": "/s3/get/bucket" } } }, "then": { "required": ["bucketName"] } }, { "if": { "properties": { "action": { "enum": ["/glue/list", "/s3/list"] } } }, "then": { "additionalProperties": false, "properties": { "action": {}, "tags": {} } } } ], "additionalProperties": false } } }, "additionalProperties": false }
核心验证规则说明
- 服务字段约束:
service数组至少包含一个元素,仅允许"Glue"或"S3"两个值,且元素不重复 - 资源数组约束:
resources数组至少包含一个资源项,每个项必须包含action和tags字段 - Action前缀匹配:
action必须以/glue/或/s3/开头- 若
action以/glue/开头,则service必须包含"Glue";若以/s3/开头,则service必须包含"S3"
- 资源属性校验:
- 当
action为/glue/get/db时,必须提供databaseName字段 - 当
action为/s3/get/bucket时,必须提供bucketName字段 - 当
action为/glue/list或/s3/list时,仅允许存在action和tags字段,不能添加其他属性
- 当
内容的提问来源于stack exchange,提问作者Yogendra Sharma
相关产品推荐
相关产品推荐

