JSON模式验证器-基于模式不同部分中的另一个属性验证一个属性

JSON模式验证器-基于模式不同部分中的另一个属性验证一个属性,json,jsonschema,json-schema-validator,ajv,Json,Jsonschema,Json Schema Validator,Ajv,我有一个OpenAPI模式,我想在其中验证它 如果components部分中的oauth2流包含tokenUrl的string格式,则servers部分中的url应该在url值中包含https: 如果tokenUrl不存在,则它不应执行任何操作 JSON模式验证器有什么方法可以做到这一点吗 下面是供参考的模式 { "servers": [ { "url": "http://my.api.serv

我有一个OpenAPI模式,我想在其中验证它

  • 如果
    components
    部分中的
    oauth2
    流包含
    tokenUrl
    string
    格式,则
    servers
    部分中的
    url
    应该在url值中包含
    https:
  • 如果
    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"
                ]
            }
        ]
    }
    

    我已经找到了实现这种验证的方法。下面是帮助您验证相同内容的模式

    {
      "$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
    应该出现在
    组件
    对象中,那么
    中定义的任何规则都将生效。下面的模式将验证
    url
    模式是否仅为
    https

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

    这里我们可以检查模式验证-