【问题标题】:Defining key-value pairs from schema to json定义从模式到 json 的键值对
【发布时间】:2014-09-13 06:30:37
【问题描述】:

我正在尝试使用 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"},
                            ]
                        }
                    }
                }


            }






    }

输出:

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

}

【问题讨论】:

    标签: json jsonschema keyvaluepair json-schema-validator


    【解决方案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",因为这看起来像是一个错误。)

    【讨论】:

    • 感谢 cloudfeet,但我仍然收到警告
    • [ { "level" : "warning", "schema" : { "loadingURI" : "#", "pointer" : "/items/properties/gum guards/items" }, "domain" : "syntax", "message" : "the following keywords are unknown and will be ignored: [Color, color code, product code]", "ignored" : [ "Color", "product code", "s-code" ] } ]
    • 当我同时验证 JSON 输出和架构时,我还会收到以下错误:
    • "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" ] } ]
    • 这可能是因为我需要“颜色、产品代码、颜色代码”的值作为键,并且它们的结果为“值”,如我上面的示例所示,结果为键值三元组,这可能吗?
    猜你喜欢
    • 2023-01-11
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2017-07-12
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多