是否可以使用allOf(如果是,则多个)和$ ref创建一个JSON模式? [英] Is it possible to create a JSON Schema with allOf (multiple if and then) and $ref?

查看:144
本文介绍了是否可以使用allOf(如果是,则多个)和$ ref创建一个JSON模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个复杂的架构,该架构将检查属性的值,然后根据该属性的值进行验证.我想知道是否可以在同一模式中使用$ ref和allOf,如果可以,如何?我在使它工作时遇到了一些麻烦.可能需要注意的是,我正在使用AJV.请在下面查看我的代码

I am trying to create a complex schema that will check for the value of a property and then validate according to the value of that same property. I am wondering if it's possible to use $ref and allOf in the same schema and if so, how? I am having some trouble getting this to work. It may be important to note that I am using AJV. Please see my code below

{ 
  "$ref": "#/definitions/Welcome",
  "definitions": {
    "Welcome": {
      "properties": {
        "auth": {
          "type": "string",
          "enum": ["oauth1","oauth2"]
        },
        "environment": {
          "$ref": "#/definitions/Environment"
        }
      }
    },
    "Environment": {
      "properties": {
        "dev": {
          "type": "object"
        }
      }
    },
    "Oauth1": {
      "type": "object",
      "properties": {
        "temporary_credentials": {
          "type": "string"
        }
      }
    },
    "Oauth2": {
      "type": "object",
      "properties": {
        "auth_url": {
          "type": "string"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "auth": {
          "const": "oauth1"
        }
      },
      "then": {
        "environment": {
          "dev": {
            "$ref": "#/definitions/Oauth1
          }
        }
      }
    },
    {
      "if": {
        "auth": {
          "const": "oauth2"
        }
      },
      "then": {
        "environment": {
          "dev": {
            "$ref": "#/definitions/Oauth2
          }
        }
      }
    }
  ]
}

要针对 this 模式进行验证的示例json输入将是这样的

A sample json input to be validated against this schema would be something like this

{
  "auth": "oauth1",
  "environment": {
    "dev": {
      "temporary_credentials": "xyzzy"
    }
  }
}

我觉得我的"then"语句或allOf的位置可能有错误.我会收到类似"$ ref:在路径#"的模式中忽略的关键字"之类的错误.

I feel like there might be an error in my "then" statements or simply the placement of the allOf. The error I would get is something like this "$ref: keywords ignored in schema at path "#"".

推荐答案

在包含draft7以及更高版本的架构版本中,一旦使用"$ref",该架构级别的所有其他关键字都将被忽略.这就是错误告诉您的内容:因为您使用了$ref,所以其他关键字将被忽略.

In schema version up to and including draft7, once you use "$ref", all other keywords in that level of the schema are ignored. That's what the error is telling you: because you used $ref, other keywords are ignored.

如果只想在根级别使用$ref,诀窍是将其包装在"allOf"中.

If you only want to use a $ref at the root level, the trick is to wrap it in an "allOf".

但是,由于您已经在根级别拥有allOf,因此您只需将$ref添加为allOf的另一个分支,它就会起作用.

But since you already have an allOf at the root level, you can just add the $ref as another branch of the allOf and it will work.

这看起来像:

"allOf": [
{
  "$ref": "#/definitions/Welcome",
},
{
  "if": {
    "auth": {
      "const": "oauth1"
    }
    etc.

注意:在发布的架构中,您有两个未闭合的字符串"#/definitions/Oauth1"#/definitions/Oauth2.如果您在实际架构中使用了该格式,那么它将是无效的JSON.

Note: in the schema you posted, you have two unclosed strings "#/definitions/Oauth1 and "#/definitions/Oauth2. If you had that in your real schema it would be invalid JSON.

这篇关于是否可以使用allOf(如果是,则多个)和$ ref创建一个JSON模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆