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