当属性存在时,在json模式中添加模式验证

当属性存在时,在json模式中添加模式验证,json,jsonschema,Json,Jsonschema,下面是我的模式定义,我想添加依赖于环境propertyName(env1、env2或env3)的模式。每个环境都应该有不同的模式。例如,当存在env1时,url将具有与存在env2时不同的模式,等等 { "environments": { "env1": { "defaultAccess": { "url": [ "something-stagin

下面是我的模式定义,我想添加依赖于环境propertyName(env1、env2或env3)的模式。每个环境都应该有不同的模式。例如,当存在env1时,url将具有与存在env2时不同的模式,等等

{
  "environments": {
    "env1": {
      "defaultAccess": {
        "url": [
          "something-staging"
        ]
      }
    }
  }
}
我当前对该示例的模式定义

    {
    "$schema": "https://json-schema.org/draft-07/schema#",
    "definitions": {
        "envType": {
            "type": "object",
            "properties": {

                "defaultAccess": {
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "pattern": "^[a-zA-Z0-9- \/]*$"

                        }
                    },
                    "required": [
                        "url"
                    ]
                }
            }
        },
        "environmentTypes": {
            "type": "object",
            "properties": {
                "env1": {
                    "$ref": "#/definitions/envType"

                },
                "env2": {
                    "$ref": "#/definitions/envType"
                },
                "env3": {
                    "$ref": "#/definitions/envType"
                }
            }
        },

        "type": "object",
        "properties": {
            "environments": {
                "$ref": "#/definitions/environmentTypes"
            }
        }
    }
}
在我的头脑中,我有这样的想法,但不知道如何正确地将其应用于模式

{
      "if": {
        "properties": {
          "environments": {
            "env1" : {}
          }
        }
      },
      "then":{
        "properties": {
          "environments-env1-defaultAccess-url" : { "pattern": "^((?!-env2).)*$" }
        }
      }
    }

等等

如果你正确地理解了你要做的事情,你就不应该需要这种事情的条件

您的模式中有一个错误,可能会让您绊倒。您的主模式位于
定义
关键字中。如果通过验证器运行此操作,您应该会得到一个错误,指出值a
/definitions/type
必须是一个对象

除此之外,使用
allOf
的模式组合应该可以做到这一点。下面,我在
/definitions/env1Type
中展示了一个示例

{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "environments": { "$ref": "#/definitions/environmentTypes" }
  },
  "definitions": {
    "environmentTypes": {
      "type": "object",
      "properties": {
        "env1": { "$ref": "#/definitions/env1Type" },
        "env2": { "$ref": "#/definitions/env2Type" },
        "env3": { "$ref": "#/definitions/env3Type" }
      }
    },
    "envType": { ... },
    "env1Type": {
      "allOf": [{ "$ref": "#/definitions/envType" }],
      "properties": {
        "defaultAccess": {
          "properties": {
            "url": { "pattern": "^((?!-env1).)*$" }
          }
        }
      }
    },
    "env2Type": { ... },
    "env3Type": { ... }
  }
}
看起来您希望用一种不太冗长的方式在对象结构的深处指定模式(
)。不幸的是,没有办法像我在
/definitions/env1Type
中演示的那样,将
属性
关键字一路链接下去

{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "environments": { "$ref": "#/definitions/environmentTypes" }
  },
  "definitions": {
    "environmentTypes": {
      "type": "object",
      "properties": {
        "env1": { "$ref": "#/definitions/env1Type" },
        "env2": { "$ref": "#/definitions/env2Type" },
        "env3": { "$ref": "#/definitions/env3Type" }
      }
    },
    "envType": { ... },
    "env1Type": {
      "allOf": [{ "$ref": "#/definitions/envType" }],
      "properties": {
        "defaultAccess": {
          "properties": {
            "url": { "pattern": "^((?!-env1).)*$" }
          }
        }
      }
    },
    "env2Type": { ... },
    "env3Type": { ... }
  }
}