【问题标题】:The property of type String did not match the following type: object in schema JSON Schema validatorString 类型的属性与以下类型不匹配:模式 JSON 模式验证器中的对象
【发布时间】:2020-03-15 19:25:45
【问题描述】:

我正在尝试为我的 JSON 对象编写 JSON 架构,但我无法跟踪错误。 我希望我的 JSON 对象在 Java 中以下列方式存储:

public class Category {
   private Map<String, List<String>> categoryMapping;
}

示例 JSON:

{
  "categoryMapping": {
    "categoryA": ["a","b","c"],
    "categoryB": ["x","y","z"],
    "categoryC": ["x","y","z"]
  }
}

但是,如果我按以下方式编写架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "title": "$id$",
  "description": "list_of_values-1",
  "required": [
    "categoryMapping"
  ],
  "properties": {
    "categoryMapping": {
      "$id": "#/properties/categoryMapping",
      "type": "object",
      "title": "The categoryMapping Schema",
      "properties": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
    }
  }
}

我收到以下错误:String 类型的属性“#/properties/categoryMapping/properties/type”与以下类型不匹配:架构中的对象http://json-schema.org/draft-04/schema#

但如果我指定了它的工作类别类型:

"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"title": "$id$",
"description": "list_of_values-1",
"properties": {
    "categoryMapping": {
      "$id": "#/properties/categoryMapping",
      "type": "object",
      "title": "The Categorymapping Schema",
      "required": [
        "categoryA",
        "categoryB",
        "categoryC"
      ],
      "properties": {
        "categoryA": {
          "$id": "#/properties/categoryMapping/properties/categoryA",
          "type": "array",
          "title": "The Categorya Schema",
          "items": {
            "$id": "#/properties/categoryMapping/properties/categoryA/items",
            "type": "string",
            "title": "The Items Schema",
            "default": "",
            "examples": [
              "a",
              "b",
              "c"
            ],
            "pattern": "^(.*)$"
          }
        },
        "categoryB": {
          "$id": "#/properties/categoryMapping/properties/categoryB",
          "type": "array",
          "title": "The Categoryb Schema",
          "items": {
            "$id": "#/properties/categoryMapping/properties/categoryB/items",
            "type": "string",
            "title": "The Items Schema",
            "default": "",
            "examples": [
              "x",
              "y",
              "z"
            ],
            "pattern": "^(.*)$"
          }
        },
        "categoryC": {
          "$id": "#/properties/categoryMapping/properties/categoryC",
          "type": "array",
          "title": "The Categoryc Schema",
          "items": {
            "$id": "#/properties/categoryMapping/properties/categoryC/items",
            "type": "string",
            "title": "The Items Schema",
            "default": "",
            "examples": [
              "x",
              "y",
              "z"
            ],
            "pattern": "^(.*)$"
          }
        }
      }
    }
  }
}

有没有办法在不明确指定所有类别类型的列表的情况下编写架构?

【问题讨论】:

    标签: java json jsonschema json-schema-validator


    【解决方案1】:

    因此,您的示例 JSON 实际上是具有三个属性的类型,这就是为什么您生成的架构要求您明确定义每个属性,即使它们实际上属于同一类型。

    如果您愿意稍微修改一下您的示例 json:

    {
      "categoryMapping": [
        {
          "name": "categoryA",
          "map": ["a","b","c"]
        },
        {
          "name": "categoryB",
          "map": ["x","y","z"]
        },
        {
          "name": "categoryC",
          "map": ["x","y","z"]
        }
      ]
    }
    

    然后您可以使用以下架构对其进行验证:

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "categoryMapping": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "map"
            ],
            "properties": {
              "name": {
                "type": "string"
              },
              "map": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
    

    由于您可以指定数组中允许的最小和最大项目数,因此您可以将类别数限制为 3,如果您愿意,也可以将“地图”的数量限制为 3。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2016-10-29
      • 2021-11-28
      • 1970-01-01
      相关资源
      最近更新 更多