JSON模式-如何使用oneOf [英] JSON schema - how to use oneOf
问题描述
以下是根据 http://jsonlint.com/和
根据上述架构进行验证时,以下JSON报告错误( The following JSON reports an error ( 任何人都可以建议我如何解决此错误吗? Can anyone suggest how I might resolve this error? 在这种情况下,您想要的是 It looks like what you want in this case is 但是,问题是如何正确使用 But, the question was how to use 这篇关于JSON模式-如何使用oneOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!results is the wrong type
):results is the wrong type
) when validated against the above schema:{
"results" : {
"result": "1"
}
}
推荐答案
enum
而不是oneOf
.这是定义架构的方式.enum
rather than oneOf
. Here is how you would define your schema.{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["results"],
"additionalProperties": false,
"properties": {
"results": {
"type": "object",
"properties": {
"result": {
"type": "string",
"enum": ["1", "2", "3", "4"]
}
}
}
}
}
oneOf
. oneOf
关键字应该是一个模式数组,而不是您在示例中使用的值. oneOf
中的一个且仅一个模式必须针对oneOf
子句的数据进行验证.我必须对您的示例进行一些修改以说明如何使用oneOf
.此示例允许result
是字符串或整数.oneOf
properly. The oneOf
keyword should be an array of schemas, not values as you have used in your example. One and only one of the schemas in oneOf
must validate against the data for the oneOf
clause to validate. I have to modify your example a little to illustrate how to use oneOf
. This example allows result
to be a string or an integer.{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["results"],
"additionalProperties": false,
"properties": {
"results": {
"type": "object",
"properties": {
"result": {
"oneOf": [
{
"type": "string",
"enum": ["1", "2", "3", "4"]
},
{
"type": "integer",
"minimum": 1,
"maximum": 4
}
]
}
}
}
}
}