oneOf 对象的 Json Schema 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25014650/
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 17:27:29  来源:igfitidea点击:

Json Schema example for oneOf objects

jsonschemajsonschema

提问by Stanimirovv

I am trying to figure out how oneOf works by building a schema which validates two different object types. For example a person (firstname, lastname, sport) and vehicles (type, cost).

我试图通过构建一个验证两种不同对象类型的模式来弄清楚 oneOf 是如何工作的。例如一个人(名字、姓氏、运动)和车辆(类型、成本)。

Here are some sample objects:

以下是一些示例对象:

{"firstName":"John", "lastName":"Doe", "sport": "football"}

{"vehicle":"car", "price":20000}

The question is what have I done wrongly and how can I fix it. Here is the schema:

问题是我做错了什么,我该如何解决。这是架构:

{
    "description": "schema validating people and vehicles", 
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "oneOf" ],
    "properties": { "oneOf": [
        {
            "firstName": {"type": "string"}, 
            "lastName": {"type": "string"}, 
            "sport": {"type": "string"}
        }, 
        {
            "vehicle": {"type": "string"}, 
            "price":{"type": "integer"} 
        }
     ]
   }
}

When I try to validate it in this parser:

当我尝试在这个解析器中验证它时:

https://json-schema-validator.herokuapp.com/

I get the following error:

我收到以下错误:

   [ {
  "level" : "fatal",
  "message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n  \"level\" : \"error\",\n  \"schema\" : {\n    \"loadingURI\" : \"#\",\n    \"pointer\" : \"/properties/oneOf\"\n  },\n  \"domain\" : \"syntax\",\n  \"message\" : \"JSON value is of type array, not a JSON Schema (expected an object)\",\n  \"found\" : \"array\"\n} ]",
  "info" : "other messages follow (if any)"
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/oneOf"
  },
  "domain" : "syntax",
  "message" : "JSON value is of type array, not a JSON Schema (expected an object)",
  "found" : "array"
} ]

回答by jruizaranguren

Try this:

尝试这个:

{
    "description" : "schema validating people and vehicles",
    "type" : "object",
    "oneOf" : [{
        "properties" : {
            "firstName" : {
                "type" : "string"
            },
            "lastName" : {
                "type" : "string"
            },
            "sport" : {
                "type" : "string"
            }
        },
        "required" : ["firstName"]
    }, {
        "properties" : {
            "vehicle" : {
                "type" : "string"
            },
            "price" : {
                "type" : "integer"
            }
        },
        "additionalProperties":false
    }
]
}

回答by Arian Kiehr

oneOfneed to be used inside a schemato work.

oneOf需要在模式中使用才能工作。

Inside properties, it's like another property called "oneOf" without the effect you want.

properties内部,它就像另一个名为“oneOf”的属性,没有您想要的效果。