"anyof"和"anyof"之间有什么区别?和“一个"在z模式中? [英] What is the difference between "anyof" and "oneof" in z schema?

查看:442
本文介绍了"anyof"和"anyof"之间有什么区别?和“一个"在z模式中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来我的输入验证代码都可以正常工作.那到底有什么区别呢?

Its looking like both works fine with my input validation code. Then what is the exact difference?

具有以下其中之一的架构

[{
  "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模式文档,它说:

5.5.4.任何

5.5.4. anyOf

5.5.4.1.有效值

5.5.4.1. Valid values

此关键字的值必须为数组.此数组必须至少包含一个元素.

This keyword's value MUST be an array. This array MUST have at least one element.

数组的元素必须是对象.每个对象都必须是有效的JSON模式.

Elements of the array MUST be objects. Each object MUST be a valid JSON Schema.

5.5.4.2.成功验证的条件

5.5.4.2. Conditions for successful validation

如果实例针对此关键字的值定义的至少一个架构成功验证了实例,则针对该关键字成功验证了实例.

An instance validates successfully against this keyword if it validates successfully against at least one schema defined by this keyword's value.

5.5.5.一个人

5.5.5.1.有效值

5.5.5.1. Valid values

此关键字的值必须为数组.此数组必须至少包含一个元素.

This keyword's value MUST be an array. This array MUST have at least one element.

数组的元素必须是对象.每个对象都必须是有效的JSON模式.

Elements of the array MUST be objects. Each object MUST be a valid JSON Schema.

5.5.5.2.成功验证的条件

5.5.5.2. Conditions for successful validation

如果实例针对此关键字的值定义的完全相同的架构成功验证了实例,则针对该关键字成功验证了

An instance validates successfully against this keyword if it validates successfully against exactly one schema defined by this keyword's value.

请注意我在上面的强调. anyOf表示该项目必须针对至少一个(但可能一个以上)模式进行验证. oneOf意味着它必须仅针对一个模式进行验证.

Note my emphasis in the above. anyOf means the item must validate against at least one (but possibly more than one) of the schemas. oneOf means it must validate against only one of the schemas.

这篇关于"anyof"和"anyof"之间有什么区别?和“一个"在z模式中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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