JSON Schema 中的“必需”与“可选”有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16204871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:42:27  来源:igfitidea点击:

What is the difference between "required" vs "optional" in JSON Schema

jsonjsonschema

提问by Cory

Sometimes, I noticed the following JSON Schemas:

有时,我注意到以下 JSON 模式:

{
    "type": "object",   
    "properties": {
        "address": {
                   "type": "string",
                   "required": true
            }
     }

}

vs

对比

{
    "type": "object",   
    "properties": {
        "address": {
                   "type": "string",
                   "optional": false
            }
     }

}

So what is the difference between requiredvs optionalin the above example?

那么上面例子中的requiredvs有什么区别optional呢?

采纳答案by Explosion Pills

This means that the object must have a non-undefinedvalue for the addressproperty (if required).

这意味着该对象undefinedaddress属性 (if required)必须具有非值。

回答by Peace Makes Plenty

The IETF draft v4of the JSON schema only defines requiredand does not include optional.

JSON 模式的IETF 草案 v4仅定义required而不包括optional.

To quote the sectionon requiredfrom draft v4:

引述部分required从第4版草案:

Valid values:The value of this keyword MUST be an array. This array MUST have at least one element. Elements of this array MUST be strings, and MUST be unique.

Conditions for successful validation:An object instance is valid against this keyword if its property set contains all elements in this keyword's array value.

有效值:该关键字的值必须是一个数组。这个数组必须至少有一个元素。该数组的元素必须是字符串,并且必须是唯一的。

成功验证的条件:如果对象实例的属性集包含此关键字数组值中的所有元素,则该对象实例对该关键字有效。

In effect, using requiredmakes optional all properties for which the name is not included in the given array of strings.

实际上, usingrequired使名称未包含在给定字符串数组中的所有属性变为可选。

回答by cloudfeet

Actually, they are equivalent expressions, but using different versions of the standard.

实际上,它们是等效的表达式,只是使用了不同版本的标准。

optionalis from v2, requiredis from v3. You should make sure you are using the correct one for your tool (although ideally you should move to v4 if you can).

optional来自 v2,required来自 v3。您应该确保为您的工具使用正确的工具(尽管理想情况下,如果可以的话,您应该迁移到 v4)。