如何从同一JSON文件中的数组中$ref对象
我想声明并定义一个我自己的对象数组,但我在按照预期的方式验证实例时遇到了问题 My如何从同一JSON文件中的数组中$ref对象,json,jsonschema,Json,Jsonschema,我想声明并定义一个我自己的对象数组,但我在按照预期的方式验证实例时遇到了问题 My$ref指向同一架构中的对象(如此处所指定,您只能指向架构;) 我跟随这个链接寻求指导 我在这里验证 我希望通过在feeder\u tx数组中不允许更多其他类型的元素,使模式更加严格,但似乎无法获得正确的语法 这是模式: { "id": "params-schema", "$schema": "http://json-schema.org/draft-04/schema#", "descri
$ref
指向同一架构中的对象(如此处所指定,您只能指向架构;)
我跟随这个链接寻求指导
我在这里验证
我希望通过在feeder\u 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
,验证器会投诉“lala”
和“max_通道”
这是有意义的,因为下一个级别是“info”
如果我试图使实例引用“info”
,则会发生以下错误:
[ {
"level" : "fatal",
"message" : "URI \"params-schema#\" is not absolute",
"uri" : "params-schema#",
"info" : "other messages follow (if any)"
} ]
[ {
"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\u tx/items/info
对象,因为我是$ref
对象
因此,我将实例数据还原回来并删除此对象。出现以下错误:
[ {
"level" : "fatal",
"message" : "URI \"params-schema#\" is not absolute",
"uri" : "params-schema#",
"info" : "other messages follow (if any)"
} ]
[ {
"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"
]
}
尝试使用:
这项工作:
{
"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"
]
}
尝试使用:
即使是我的原始模式也会在该站点上进行验证,不管实例中是否指定了“info”。所以我不确定它有多严格。它会验证,因为它是有效的……但是如果你需要更多的测试,它也会在这里验证:这里:(在最后一个测试中,将id更改为“#”)。您使用的验证器是什么?我在顶部声明我使用的是“”。现在我已经将“id”更改为“#”,它还可以验证,谢谢。这有什么意义?即使是我的原始模式也会在该站点上进行验证,不管实例中是否指定了“info”。所以我不确定它有多严格。它会验证,因为它是有效的……但是如果你需要更多的测试,它也会在这里验证:这里:(在最后一个测试中,将id更改为“#”)。您使用的验证器是什么?我在顶部声明我使用的是“”。现在我已经将“id”更改为“#”,它还可以验证,谢谢。这有什么意义?