我想声明并定义一个自己的对象数组,但是我遇到了以我期望的方式验证实例的问题。
我的$ref
指向同一架构中的对象(此处指定的只能指向架构; Can JSON integer attributes be referenced?)
我已按照此链接获取指导; https://spacetelescope.github.io/understanding-json-schema/structuring.html
我在这里验证; http://json-schema-validator.herokuapp.com/
我想通过不允许feeder_tx
数组中的其他类型的其他元素来使模式更严格,但似乎无法使语法正确。
这是架构:
{
"id": "params-schema",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "my schema",
"definitions": {
"tx": {
"type": "object",
"properties": {
"comment": {"type": "string"},
"max_channels": {
"type": "integer",
"minimum" : 0,
"maximum" : 10000,
"additionalProperties": false
}
},
"additionalProperties": false,
"required": ["max_channels"]
}
},
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"type": "object",
"properties": {
"comment": {"type": "string"},
"info": {
"$ref": "#/definitions/tx",
"additionalProperties": false
}
}
}
}
},
"required": ["feeder_tx"]
}
这是实例:
{
"feeder_tx": [
{"lala": 25, "max_channels": 1499}
]
}
由于添加了lala
,我想失败,但它会成功验证。
如果我在"additionalProperties": false
的{{1}}部分末尾添加"properties"
,则验证者会同时抱怨items
和"lala"
。
这是有道理的,因为下一级是"max_channels"
"info"
如果我尝试让实例引用[ {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/feeder_tx/items"
},
"instance" : {
"pointer" : "/feeder_tx/0"
},
"domain" : "validation",
"keyword" : "additionalProperties",
"message" : "object instance has properties which are not allowed by the schema: [\"lala\",\"max_channels\"]",
"unwanted" : [ "lala", "max_channels" ]
} ]
,则会发生以下错误:
"info"
此实例数据:
[ {
"level" : "fatal",
"message" : "URI \"params-schema#\" is not absolute",
"uri" : "params-schema#",
"info" : "other messages follow (if any)"
} ]
事实上,我不明白为什么我需要一个{
"feeder_tx": [
{"info": { "max_channels": 1499}}
]
}
对象,因为我feeder_tx/items/info
是一个对象。
所以我还原了实例数据并删除了这个对象。发生以下错误:
$ref
即架构和实例变为:
[ {
"level" : "fatal",
"message" : "URI \"params-schema#\" is not absolute",
"uri" : "params-schema#",
"info" : "other messages follow (if any)"
} ]
有人可以解释一下这里发生了什么,以及正确的方法吗?
重构架构以不使用"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"$ref": "#/definitions/tx"
}
}
},
可以解决问题,但我想知道如何使用引用。
$ref
给出了正确的验证错误:
{
"id": "params-schema",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "parameters schema",
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"type": "object",
"properties": {
"comment": {"type": "string"},
"max_channels": {
"type": "integer",
"minimum" : 0,
"maximum" : 10000,
"additionalProperties": false
}
},
"additionalProperties": false,
"required": ["max_channels"]
}
}
},
"required": ["feeder_tx"]
}
感谢。
答案 0 :(得分:2)
这有效:
{
"id": "#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "parameters schema",
"definitions": {
"item": {
"type": "object",
"properties": {
"comment": {
"type": "string"
},
"max_channels": {
"type": "integer",
"minimum": 0,
"maximum": 10000,
"additionalProperties": false
}
},
"additionalProperties": false,
"required": [
"max_channels"
]
}
},
"type": "object",
"properties": {
"feeder_tx": {
"type": "array",
"minItems": 1,
"maxItems": 4,
"items": {
"$ref": "#/definitions/item"
}
}
},
"required": [
"feeder_tx"
]
}
尝试使用: