如何在 json 架构中创建架构引用的嵌套列表(数组)
How do I do a nested list (array) of schema references in json schema
我正在尝试构建一个架构,其中包含我希望具有强制架构的项目列表。
基本上这是我要根据架构验证的数据:
data = {
"VIN" : "123",
"timestamp" : "xxxx",
"model" : "jeep",
"inspections": [
{ "door_badge" :
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
},
},
{
"rear_badge" :
{
"expected" : "yes",
"image" : "/image/path/here",
"state": 1
}
}
],
}
我的模式映射是这样的,但在尝试验证时似乎出错了:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type": "string" },
"found": { "type" : "string"},
"state" : { "enum" : [0,1] },
"image" : { "type" : "string"}
},
"required": ["state", "image"]
},
"inspections" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/inspection"
},
"required" : ["items"]
},
},
"type" : "object",
"properties" : {
"VIN" : { "type" : "string" },
"timestamp" : { "type" : "string"},
"model" : { "type" : "string"},
"inspections" : { "$ref" : "#/definitions/inspections"}
},
"required": ["VIN", "timestamp", "model"]
}
我基本上想要检查列表中的子列表,但也要根据该类型的项目进行验证。
解决方案:
感谢 jruizaranguren 的帮助,解决方案是:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type": "string" },
"found": { "type" : "string"},
"state" : { "enum" : [0,1] },
"image" : { "type" : "string"}
},
"required": ["state", "image", "expected"]
},
},
"type" : "object",
"properties" : {
"VIN" : { "type" : "string" },
"timestamp" : { "type" : "string"},
"model" : { "type" : "string"},
"inspections" : {
"type" : "array",
"items" : {
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
}
},
"required": ["VIN", "timestamp", "model", "inspections"]
}
您遇到的问题是您将数组中的每个项目限制为以下形式:
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
}
但是您的对象是以下形式:
{ "door_badge" :
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
},
}
实现此目的的一种方法是在 items
子句中使用 additionalProperties
:
"items" :
{
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
如果您可以对这些属性键执行一些规则(例如,所有键都必须以 badge 结尾),那么您可以使用带有正则表达式的 patternProperties
子句。
我正在尝试构建一个架构,其中包含我希望具有强制架构的项目列表。
基本上这是我要根据架构验证的数据:
data = {
"VIN" : "123",
"timestamp" : "xxxx",
"model" : "jeep",
"inspections": [
{ "door_badge" :
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
},
},
{
"rear_badge" :
{
"expected" : "yes",
"image" : "/image/path/here",
"state": 1
}
}
],
}
我的模式映射是这样的,但在尝试验证时似乎出错了:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type": "string" },
"found": { "type" : "string"},
"state" : { "enum" : [0,1] },
"image" : { "type" : "string"}
},
"required": ["state", "image"]
},
"inspections" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/inspection"
},
"required" : ["items"]
},
},
"type" : "object",
"properties" : {
"VIN" : { "type" : "string" },
"timestamp" : { "type" : "string"},
"model" : { "type" : "string"},
"inspections" : { "$ref" : "#/definitions/inspections"}
},
"required": ["VIN", "timestamp", "model"]
}
我基本上想要检查列表中的子列表,但也要根据该类型的项目进行验证。
解决方案: 感谢 jruizaranguren 的帮助,解决方案是:
schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"inspection": {
"type": "object",
"properties": {
"expected" : { "type": "string" },
"found": { "type" : "string"},
"state" : { "enum" : [0,1] },
"image" : { "type" : "string"}
},
"required": ["state", "image", "expected"]
},
},
"type" : "object",
"properties" : {
"VIN" : { "type" : "string" },
"timestamp" : { "type" : "string"},
"model" : { "type" : "string"},
"inspections" : {
"type" : "array",
"items" : {
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
}
},
"required": ["VIN", "timestamp", "model", "inspections"]
}
您遇到的问题是您将数组中的每个项目限制为以下形式:
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
}
但是您的对象是以下形式:
{ "door_badge" :
{
"expected": "yes",
"image": "/image/path/here",
"state": 1
},
}
实现此目的的一种方法是在 items
子句中使用 additionalProperties
:
"items" :
{
"type" : "object",
"maxProperties": 1,
"minProperties": 1,
"additionalProperties" : {
"$ref" : "#/definitions/inspection"
}
}
如果您可以对这些属性键执行一些规则(例如,所有键都必须以 badge 结尾),那么您可以使用带有正则表达式的 patternProperties
子句。