类型为String的属性与以下类型不匹配:JSON模式中的对象Schema验证程序

时间:2019-11-20 06:41:49

标签: java json jsonschema json-schema-validator

我正在尝试为我的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"
          }
        }
    }
  }
}

我收到以下错误:字符串类型的属性'#/ 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": "^(.*)$"
          }
        }
      }
    }
  }
}

是否可以在不显式指定所有类别类型的列表的情况下编写模式?

1 个答案:

答案 0 :(得分: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。