我正在尝试使用来自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:
{
"$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"
}
}
答案 0 :(得分:1)
问题在于:
"gum guards" : {
"type": "object",
您已声明"gum guards"
必须是对象,例如:
"gum guards": {"Color" : ...},
如果你想要"牙龈护理"要成为一个数组,然后使用"type": "array"
,并使用"items"
指定项目的架构:
"gum guards": {
"type": "array",
"items": {
"type": "object",
"properties": {...},
"required": ["Color", "product code", "color code"]
}
}
(我还将"enum"
更正为"required"
,因为这看起来像是一个错误。)