首页 文章

定义从schema到json的键值对

提问于
浏览
2

我正在尝试使用来自JSON模式的键值对定义一个对象并在以下位置验证它:Json Schema Validator但我没有任何乐趣,因为在我查找的所有JSON模式站点中似乎没有这样做的指令 .

我的对象模式定义如下:

"gum guards" : {
                    "type": "object",

                        "properties": {
                        "Color":      { "type": "string" },
                        "product code": { "type": "string" },
                        "color code": { "type": "string"}
                     },
                    "enum" : ["Color", "product code", "color code"]
                }

生成的JSON文件应该给我以下值:

"gum guards" : [
    { "Color" : "Black", "product code" : "gg-7890", "color code" : "#000000" },
    { "Color" : "White", "product code" : "gg-7891", "color code" : "#ffffff" }
]

但是,验证器给出了以下错误消息:

[ {
  "level" : "error",
  "schema" : {
   "loadingURI" : "#",
   "pointer" : ""
 },
  "instance" : {
   "pointer" : ""
  },
  "domain" : "validation",
  "keyword" : "type",
  "message" : "instance type (object) does not match any allowed primitive type (allowed:          [\"array\"])",
   "found" : "object",
    "expected" : [ "array" ]
    } ]

如何在JSON模式中使用键值/对定义数组?

架构:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "List of products",
"type": "array",

    "items": {
        "title": "Product",
        "type": "object",
            "properties": {
                "id": {
                    "description": "The unique identifier for a product",
                    "type": "number"
                },
                "Category" : {
                    "type": "string"
                },
                "Product Name" : {
                    "type" : "string"
                },

                "gum guards" : {
                    "type": "array",

                        "items": {
                           "Color": { "type": "string" },
                           "product code": { "type": "string" },
                           "color code": { "type": "string"}
                         },
                    "required" : ["Color", "product code", "color code"]
                },
                "Summary" : {
                "type": "object",
                    "properties": {
                        "Description": {
                            "oneOf": [
                                {"$ref" : "json/product_summary.json#1110/description"},
                                {"$ref" : "json/product_summary.json#1111/description"},
                                {"$ref" : "json/product_summary.json#1112/description"},
                                {"$ref" : "json/product_summary.json#1114/description"},
                            ]
                        }
                    }
                }


            }






    }

OUTPUT:

{
"id" : 1110,
"Device Type" : "handset",
"Product Name" : "Pack of accessories",
"variants" : [
    { "Color" : "Black", "product code" : "gg-09090", "color code" : "#000000" },
    { "Color" : "White", "product code" : "gg-09091", "color code" : "#ffffff" }
],
"Summary" : {

    "description" : "Pack of fighter products with chosen colour guard"
}

}

1 回答

  • 1

    问题在于:

    "gum guards" : {
                    "type": "object",
    

    你已声明 "gum guards" 必须是一个对象,如:

    "gum guards": {"Color" : ...},
    

    如果希望"gum guards"成为数组,则使用 "type": "array" ,并使用 "items" 指定项目的模式:

    "gum guards": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {...},
            "required": ["Color", "product code", "color code"]
        }
    }
    

    (我也将 "enum" 更正为 "required" ,因为这看起来像是一个错误 . )

相关问题