z 架构中的 "anyof" 和 "oneof" 有什么区别?
What is the difference between "anyof" and "oneof" in z schema?
它看起来都可以正常使用我的输入验证码。那么具体的区别是什么?
其中一个架构
[{
"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"}
}
}
]
架构与任何
[{
"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"}
}
}
]
如果您查看 JSON 模式文档,它说:
...
An instance validates successfully against this keyword if it
validates successfully against at least one schema defined by this
keyword's value. Note that when annotations are being collected, all
subschemas MUST be examined so that annotations are collected from
each subschema that validates successfully.
...
An instance validates successfully against this keyword if it
validates successfully against exactly one schema defined by this
keyword's value.
注意我上面强调的重点。 anyOf
表示该项目必须针对至少一个(但可能 多个 )模式进行验证。 oneOf
意味着它必须针对 只有一个 模式进行验证。
我迟到了,但根据我的理解,此关键字的用法取决于 object/parent 本身的类型。例如,如果您尝试为对象的单个 属性 或数组元素定义类型。以下面的例子为例:
{
"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 是相同的(您可以互换使用 anyOf
或 oneOf
)并且您可以定义您喜欢的任何内容。但是,如果是数组:
- 当您使用
anyOf
作为项目类型时,元素可以是其中的任何类型,数组可以包含混合项目。意味着您可以拥有一个类型 1 的项目和另一个类型 2 的项目。
- 当您使用
oneOf
作为项目类型时,元素可以是其中的任何类型,并且数组只能包含一种类型的项目。表示所有项目必须属于同一类型(类型 1 或类型 2)。
它看起来都可以正常使用我的输入验证码。那么具体的区别是什么?
其中一个架构
[{
"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"}
}
}
]
架构与任何
[{
"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"}
}
}
]
如果您查看 JSON 模式文档,它说:
...
An instance validates successfully against this keyword if it validates successfully against at least one schema defined by this keyword's value. Note that when annotations are being collected, all subschemas MUST be examined so that annotations are collected from each subschema that validates successfully.
...
An instance validates successfully against this keyword if it validates successfully against exactly one schema defined by this keyword's value.
注意我上面强调的重点。 anyOf
表示该项目必须针对至少一个(但可能 多个 )模式进行验证。 oneOf
意味着它必须针对 只有一个 模式进行验证。
我迟到了,但根据我的理解,此关键字的用法取决于 object/parent 本身的类型。例如,如果您尝试为对象的单个 属性 或数组元素定义类型。以下面的例子为例:
{
"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 是相同的(您可以互换使用 anyOf
或 oneOf
)并且您可以定义您喜欢的任何内容。但是,如果是数组:
- 当您使用
anyOf
作为项目类型时,元素可以是其中的任何类型,数组可以包含混合项目。意味着您可以拥有一个类型 1 的项目和另一个类型 2 的项目。 - 当您使用
oneOf
作为项目类型时,元素可以是其中的任何类型,并且数组只能包含一种类型的项目。表示所有项目必须属于同一类型(类型 1 或类型 2)。