【问题标题】:JsonSchema is not validated with oneOfJsonSchema 未使用 oneOf 验证
【发布时间】:2015-01-06 13:08:32
【问题描述】:

需要帮助来查找此架构的错误。它有 oneOf 运算符。 架构在这里:

   `{
    "type": "object",
    "required": [
        "type",
        "body"
    ],
    "properties": {
        "type": {
            "description": "type of the document to post",
            "type": "string",
            "enum": [
                "123",
                "456"                    
            ]
        },
        "body": {
            "type": "object",
            "description": "body",
            "oneOf": [{
                "$ref": "#/definitions/abc",
                "$ref": "#/definitions/def"
            }]
        }
    },
    "definitions": {
        "abc": {                
            "type": "array",
            "description": "abc",
           "properties" : {
               "name" : { "type" : "string"  }
         }
        },
        "def": {                
            "type": "array",
            "description": "users","properties" : {
               "name" : { "type" : "string"  }
         }
        }
    }
}`

我的 Json 是这样的:

  `{
            "type": "123",
            "body": {
                "abc": [{
                    "name": "test"
                }]
            }
        }`

它不适用于 tv4,我也试过这个online tool。它在没有 oneOf 运算符的情况下工作。否则它不会验证任何工具。

编辑

阅读答案后,我修改了架构。新架构是:

{
"type": "object",
"properties": {
    "type": {
        "description": "type of the document to post",
        "type": "string",

    },
    "body": {
        "type": "object",
        "description": "body",
        "properties": {
            "customers": {
                "type": "array"
            }
        },
        "anyOf": [
            {
                "title": "customers prop",
                "properties": { 
                    "customers": {
                        "type": "array",
                        "description": "customers",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        ]
    }
  }
}

json 在这里

{
"type": "customer",
"body": {
    "none": [
        {
            "name": "test"
        }
    ]
  }
}

但它验证了。我想在正文中强制执行“客户”或“用户”之一。为了测试我已经从正文中删除了用户。

请帮忙。

【问题讨论】:

    标签: jsonschema tv4


    【解决方案1】:

    问题是数据正在通过您的两个子模式。 oneOf 的意思是“匹配完全一个”——如果你想“匹配至少一个”,那么使用anyOf

    事实上,您的两个子模式都会传递所有数据。原因是properties在处理数组时被忽略了。

    您可能想要做的是为数组中的 items 指定属性。为此,您需要 items 关键字:

    "definitions": {
        "abc": {                
            "type": "array",
            "items": {
                "type": "object",
                "properties" : {
                    "name" : { "type" : "string"  }
                }
            }
        }
    }
    

    (您还需要添加一些不同的约束 - 目前,"abc""def" 的定义除了 description 之外都是相同的,这使得 oneOf 不可能,因为它总是匹配两者或者两者都没有。)

    【讨论】:

      【解决方案2】:

      由于您有根级别的类型,您可能希望 oneOf 语句检查类型为“customer”的对象在主体中是否有客户(即使我建议跳过主体并将客户和用户直接放在根中对象)。

      这适用于您的示例,将要求类型为“customer”的对象具有带有“customers”的主体,为了澄清匹配,我让客户拥有属性“name”,而用户拥有“username”:

      {
        "type": "object",
        "properties": {
          "type": { "type": "string" },
          "body": {
            "type": "object",
            "properties": {
              "customers": { 
                "type": "array",
                "items": { "$ref": "#/definitions/customer" }
              },
              "users": {
                "type": "array",
                "items": { "$ref": "#/definitions/user" }
              }
            }
          }
        },
        "definitions": {
          "customer": {
            "type": "object",
            "properties": { "name": { "type": "string" } },
            "required": [ "name" ]
          },
          "user": {
            "type": "object",
            "properties": { "username": { "type": "string" } },
            "required": [ "username" ]
          }
        },
        "oneOf": [
          {
            "properties": {
              "type": {
                "pattern": "customer"
              },
              "body": {
                "required": [ "customers" ]
              }
            }
          },
          {
            "properties": {
              "type": {
                "pattern": "user"
              },
              "body": {
                "required": [ "users" ]
              }
            }
          }
        ]
      }
      

      【讨论】:

        【解决方案3】:

        当使用"type": "array" 时,项目类型在"items" 属性中定义,而不是在"properties" 属性中...而且oneOf 中的两种类型相同,但只有一个必须匹配。

        试试

              ...
              "definitions": {
                "abc": {                
                    "type": "array",
                    "description": "abc",
                   "items" : {
                       "name" : { "type" : "string"  }
                 }
                },
                "def": {                
                    "type": "array",
                    "description": "users",
                    "items" : {
                       "username" : { "type" : "string"  }
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 2022-01-04
          • 1970-01-01
          • 1970-01-01
          • 2018-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多