【问题标题】:JSON schema validator - validate one property based on another property in different part of schemaJSON 模式验证器 - 根据模式不同部分中的另一个属性验证一个属性
【发布时间】:2021-04-24 06:47:52
【问题描述】:

我有一个开放的 api 架构,我想在其中验证它

  1. 如果components 部分中的oauth2 流包含string 格式的tokenUrl,则servers 部分中的url 应该在url 值中包含https:
  2. 如果tokenUrl 不存在,那么它应该什么都不做

有没有办法通过 JSON 模式验证器实现这一点?

以下是供参考的架构

{  
    "servers": [
        {
            "url": "http://my.api.server.com/",
            "description": "API server"
        }
    ], 
    "components": {
        "securitySchemes": {
            "OAuth2": {
                "type": "oauth2",
                "flows": {
                    "authorizationCode": {
                        "scopes": {
                            "write": "modify objects in your account",
                            "read": "read objects in your account"
                        },
                        "authorizationUrl": "https://example.com/oauth/authorize",
                        "tokenUrl": "https://example.com/oauth/token" 
                    }
                }
            }
        }
    },
    "security": [
        {
        "OAuth2": [
                "write",
                "read"
            ]
        }
    ]
}

【问题讨论】:

    标签: json jsonschema json-schema-validator ajv


    【解决方案1】:

    我找到了一种方法来实现这种类型的验证。下面是帮助您验证相同的架构

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "servers": {},
        "components": {}
      },
      "if": {
        "properties": {
          "components": {
            "properties": {
              "securitySchemes": {
                "type": "object",
                "patternProperties": {
                  "^[a-zA-Z0-9\\.\\-_]+$": {
                    "type": "object",
                    "properties": {
                      "flows": {
                        "type": "object",
                        "properties": {
                          "tokenUrl": {
                            "const": true
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "then": {
        "type": "object",
        "properties": {
          "servers": {
            "items": {
              "type": "object",
              "properties": {
                "url": {
                  "type": "string",
                  "pattern": "https://"
                }
              }
            }
          }
        }
      }
    }
    

    if - then 用于设置组件tokenUrl 属性和服务器url 属性之间的条件关系。这里

    {
        "tokenUrl": {
            "const": true
        }
    }
    

    表示tokenUrl 应该存在于components 对象中,那么then 块中定义的任何规则都会生效。下面的模式将验证url 模式仅是https

    {
        "url": {
            "type": "string",
            "pattern": "https://"
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多