使用JsonSchema和$ ref验证对象的异构列表 [英] Validating Heterogeneous Lists of Objects with JsonSchema and $ref

查看:409
本文介绍了使用JsonSchema和$ ref验证对象的异构列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了类似问题的答案,这些问题确实与这种特殊情况不太匹配,如果我错过了相关内容,我们深表歉意答案.

I have seen answers to similar questions that do not quite match this particular case, so apologies if I missed a relevant answer.

我有一个要验证的异构对象数组.这些对象在顶层具有相同的格式,但是子对象完全不同,并且只能由每个子对象中存在的属性来标识.

I have a heterogeneous array of objects that I would like to validate. These objects have the same format at the top level, but the child objects are quite different and can only be identified by the attributes present in each child.

尽管我在数组中有两种以上的对象类型,但问题映射到验证以下数据:

The problem maps to validating the following data, though I have more than two object types in the array:

{
  "heterogeneous_array": [{
      "arbitrary_name": "foobar",
      "params": {
        "aa": "foo",
        "ab": "bar"
      }
    },
    {
      "arbitrary_name": "barfoo",
      "params": {
        "ba": "baz",
        "bb": "bot"
      }
    }
  ]
}

我正在使用以下架构,该架构声称即使"params"键下的对象无效,也可以验证输入json.如何修复JSON模式?

I am using the following schema, which claims to validate the input json even when the objects under the "params" key are invalid. How can I fix the json schema?

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "heterogeneous_array": {
      "$ref": "#/definitions/heterogeneous_array"
    }
  },
  "definitions": {
    "heterogeneous_array": {
      "type": "array",
      "items": {
        "arbitrary_name": {
          "type": "string"
        },
        "params": {
          "oneOf": [{
              "$ref": "#/definitions/schema_a"
            },
            {
              "$ref": "#/definitions/schema_b"
            }
          ]
        },
        "required": ["arbitrary_name", "params"]
      }
    },
    "schema_a": {
      "properties": {
        "aa": {
          "type": "string"
        },
        "ab": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": ["aa", "ab"]
    },
    "schema_b": {
      "properties": {
        "ba": {
          "type": "string"
        },
        "bb": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": ["ba", "bb"]
    }
  }
}

提前谢谢!

推荐答案

首先让我惊讶的是parametersarbitrary_name不是JSON Schema关键字.我认为您缺少一些properties关键字.

The first thing that jumps out at me is that parameters and arbitrary_name are not JSON Schema keywords. I think you're missing a few properties keywords.

尝试一下:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "heterogeneous_array": {
      "$ref": "#/definitions/heterogeneous_array"
    }
  },
  "definitions": {
    "heterogeneous_array": {
      "type": "array",
      "items": {
        "properties": {             // missing this
          "arbitrary_name": {
            "type": "string"
          },
          "params": {
            "oneOf": [{
                "$ref": "#/definitions/schema_a"
              },
              {
                "$ref": "#/definitions/schema_b"
              }
            ]
          }
        },
        "required": ["arbitrary_name", "params"]    // "arbitrary_name" was "name"
      }
    },
    "schema_a": {
      "properties": {             // was "parameters"
        "aa": {
          "type": "string"
        },
        "ab": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": ["aa", "ab"]
    },
    "schema_b": {
      "properties": {             // was "parameters"
        "ba": {
          "type": "string"
        },
        "bb": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": ["ba", "bb"]
    }
  }
}

我还有其他一些评论.

最后一点(要解决的问题)是次要的,应该加以注意,并且JSON库可能仍然支持它:JSON中的布尔值始终为小写(例如,false而不是False). (实际上,它们被定义为显式令牌.)

The last thing (to fix what you have) is minor, should be noted, and is probably supported by your JSON library anyway: boolean values in JSON are always lower-case (e.g. false not False). (They're actually defined as explicit tokens.)

您的问题尚不清楚的是foobar对象是否需要aaab参数,而barfoo对象是否需要babb参数.在这种情况下,如果您使用的是JSON Schema draft 6或更高版本,则可以做一些其他事情.

What is unclear from your question is whether the foobar object requires the aa and ab parameters while the barfoo object requires the ba and bb parameters. If this is the case, you can do some other things if you're using JSON Schema draft 6 or higher.

草稿6定义了一个const属性,您可以使用该属性来隔离特定属性的值,并对对象的其他部分实施子方案.使用此功能,您可以创建一种 switch 语句.

Draft 6 defines a const property that you can use to isolate values for specific properties and enforce subschemas on other portions of the object. Using this, you can create a sort of switch statement.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "heterogeneous_array": {
      "$ref": "#/definitions/heterogeneous_array"
    }
  },
  "definitions": {
    "heterogeneous_array": {
      "type": "array",
      "items": {
        "oneOf": [
          {"$ref": "#/definitions/schema_a"},
          {"$ref": "#/definitions/schema_b"}
        ],
        "required": ["arbitrary_name", "params"]
      }
    },
    "schema_a": {
      "properties": {
        "arbitrary_name": {"const": "foobar"},
        "params": {
          "properties": {
            "aa": {
              "type": "string"
            },
            "ab": {
              "type": "string"
            }
          },
          "additionalProperties": false,
          "required": ["aa", "ab"]
        }
      }
    },
    "schema_b": {
      "properties": {
        "arbitrary_name": {"const": "barfoo"},
        "params": {
          "properties": {
            "ba": {
              "type": "string"
            },
            "bb": {
              "type": "string"
            }
          },
          "additionalProperties": false,
          "required": ["ba", "bb"]
        }
      }
    }
  }
}

这有点重组,对于每个具有arbitrary_name的值,您都需要一个schema_?.

This is a bit of a reorganization and you'll need a schema_? for each value of arbitrary_name that you have.

此外,如果您使用的是草稿7,则还具有if/then/else关键字,但是我认为这不会使此使用案例变得更干净.

Further to this, if you're using draft 7, you also have the if/then/else keywords, but I don't think that makes this use case any cleaner.

这篇关于使用JsonSchema和$ ref验证对象的异构列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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