16

看起来两者都适用于我的输入验证代码。那么具体的区别是什么?

带有 oneof 的架构

[{
  "id": "MyAction",
  "oneOf": [{ "$ref": "A1" },
            { "$ref": "A2" }]
 },
 {
  "id": "A1",
  "properties": {
      "class1": { "type": "string"},
      "class2": { "type": "string"}
   }
 },
 {
  "id": "A2",
  "properties": {
      "class2": { "type": "string"},
      "class3": { "type": "string"}
   }
 }
]

带有 anyof 的架构

    [{
  "id": "MyAction",
  "anyOf": [{ "$ref": "A1" },
            { "$ref": "A2" }]
 },
 {
  "id": "A1",
  "properties": {
      "class1": { "type": "string"},
      "class2": { "type": "string"}
   }
 },
 {
  "id": "A2",
  "properties": {
      "class2": { "type": "string"},
      "class3": { "type": "string"}
   }
 }
]
4

2 回答 2

30

如果您查看 JSON Schema 文档,它会说:

anyOf

...

如果一个实例成功地验证了该关键字值定义的至少一个模式,则该实例成功地验证了该关键字。请注意,在收集注释时,必须检查所有子模式,以便从每个成功验证的子模式中收集注释。

oneOf

...

如果实例针对此关键字值定义的一个模式成功验证,则该实例针对此关键字成功验证。

请注意我在上面的重点。anyOf表示该项目必须针对至少一种(但可能不止一种)模式进行验证。oneOf意味着它必须仅针对其中一种模式进行验证。

于 2015-12-02T08:45:04.427 回答
4

我迟到了,但根据我的理解,这个关键字的用法取决于对象/父对象本身的类型。例如,如果您尝试为对象的单个属性或数组元素定义类型。举个例子:

{
    "title": "Sample JSON Schema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "definitions": {
        "propObjectType1" : {
            "name": "string",
            "age": "number"
        },
        "propObjectType2" : {
            "name": "string",
            "dob": {
                "type": "string",
                "pattern": "\\d\\d\/\\d\\d\/\\d\\d\\d\\d"
            }
        }
    },
    "properties": {
        "prop1": {
            "type": "string",
            "maxLength": 64
        },
        "prop2": {
            "anyOf": [
                {
                    "$ref": "#/definitions/propObjectType1"
                },
                {
                    "$ref": "#/definitions/propObjectType2"
                }
            ] 
        },
        "prop3": {
            "oneOf": [
                {
                    "$ref": "#/definitions/propObjectType1"
                },
                {
                    "$ref": "#/definitions/propObjectType2"
                }
            ] 
        },
        "prop4Array": {
            "type": "array",
            "items": {
                "oneOf": [
                    {
                        "$ref": "#/definitions/propObjectType1"
                    },
                    {
                        "$ref": "#/definitions/propObjectType2"
                    }
                ] 
            }       
        },
        "prop5Array": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "$ref": "#/definitions/propObjectType1"
                    },
                    {
                        "$ref": "#/definitions/propObjectType2"
                    }
                ] 
            }       
        }
    }
}

所以在上面的定义中, prop2 和 prop3 是相同的(你可以互换使用anyOfoneOf),你可以定义你喜欢的东西。但是,如果是数组:

  1. 当您anyOf用于项目类型时,元素可以是其中的任何类型,并且数组可以包含混合项目。意味着您可以拥有一个类型 1 的项目和另一个类型 2 的项目。
  2. 当您使用oneOfitems 类型时,元素可以是其中的任何类型,并且数组只能包含一种类型的项目。表示所有项目必须属于同一类型(类型 1 或类型 2)。
于 2019-05-08T11:33:12.200 回答