【问题标题】:Json Schema example for oneOf objectsoneOf 对象的 Json Schema 示例
【发布时间】:2014-09-20 18:52:03
【问题描述】:

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

以下是一些示例对象:

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

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

问题是我做错了什么以及如何解决它。这是架构:

{
    "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"} 
        }
     ]
   }
}

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

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

我收到以下错误:

   [ {
  "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"
} ]

【问题讨论】:

    标签: json schema jsonschema


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • oneOf 是否有助于验证请求?在 pojo 转换期间我看不到 oneOf 的任何杰克逊注释
    • @gursahib.singh.sahni,这是杰克逊特有的问题。将 oneOf 语义翻译成 java 等一些编程语言并不容易。您需要一些其他语言具有的结构,例如不相交集、可区分联合、联合类型……。
    • @jruizaranguren 除了杰克逊以外,还有其他图书馆可以处理这种情况吗?否则我们将不得不在我们的代码库中添加自定义验证器。
    • 根目录下是否需要"type" : "object"?这没有多大意义。
    • @AlirezaMirian,如果您没有明确指定它,任何其他类型都将满足规范,因为“oneOf”仅适用于类型。对于其他类型,它会被忽略。
    【解决方案2】:

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

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

    【讨论】:

    • 我没有说你的错,但是你能提供这个信息的来源吗? =]
    • 嗯,从 properties 的作用中推断出来是有意义的,即为什么 properties 会为名为 oneOf 的键创建特殊情况?现在我想到它,这几乎是常识。谢谢你的解释。
    • 这也是我发现的。属性内部的 oneOf 不起作用。
    猜你喜欢
    • 2020-06-09
    • 2020-12-01
    • 2017-06-24
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多