如何从同一 JSON 文件中的数组中 $ref 一个对象
How to $ref an object from within an array in the same JSON file
我想声明并定义一个我自己的对象数组,但我在按预期方式验证实例时遇到问题。
我的$ref
指向同一模式中的一个对象(这里指定你只能指向一个模式;)
我已按照此 link 寻求指导; 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
而失败,但它验证成功。
如果我在 items
的 "properties"
部分末尾添加一个 "additionalProperties": false
,验证器会抱怨 "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
ing 是一个对象。
所以我还原了实例数据并删除了这个对象。出现以下错误:
[ {
"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"]
}
给出正确的验证错误:
[ {
"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\"]",
"unwanted" : [ "lala" ]
} ]
谢谢。
这个有效:
{
"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"
]
}
尝试使用:
我想声明并定义一个我自己的对象数组,但我在按预期方式验证实例时遇到问题。
我的$ref
指向同一模式中的一个对象(这里指定你只能指向一个模式;
我已按照此 link 寻求指导; 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
而失败,但它验证成功。
如果我在 items
的 "properties"
部分末尾添加一个 "additionalProperties": false
,验证器会抱怨 "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
ing 是一个对象。
所以我还原了实例数据并删除了这个对象。出现以下错误:
[ {
"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"]
}
给出正确的验证错误:
[ {
"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\"]",
"unwanted" : [ "lala" ]
} ]
谢谢。
这个有效:
{
"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"
]
}
尝试使用: