Json-schema 不使用 $ref-references 验证 json
Json-schema doesn't validate json with $ref-references
我有这个json:
{
"categories": [
{
"id": 1,
"name": "cat1",
"topics": [
{
"$ref": "#/topics/1"
}
]
}
],
"topics": [
{
"id": 1,
"name": "top1"
}
]
}
我已经编写了下一个模式来验证它:
{
"definitions": {
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"topics": {
"type": "array"
"items": { "$ref": "#/definitions/topic" }
}
}
},
"topic": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
},
"type": "object",
"properties": {
"categories": {
"items": { "$ref": "#/definitions/category" },
"type": "array"
},
"topics": {
"items": { "$ref": "#/definitions/topic" },
"type": "array"
}
}
}
当我在流行的在线验证器上使用此架构时,它不会捕获 #/topics/5
或 #/ttt/555
等无效引用。
我可以使用此架构来验证引用吗?你能推荐我图书馆或服务吗?
我不确定我是否正确理解了您想要实现的目标。我假设您想表示 "topics"
数组的项目应该是 JSON 引用("$ref"
和 JSON 指针)_并且指向的对象应该匹配模式 "#/definitions/topic"
.
如果是这种情况,那么目前没有办法用 json 模式来表达它,所以 - 即使是最新版本 - 你也只能表示一个字符串应该是 json 指针,但不能限制引用对象的类型。
去年我 made a suggestion 解决了这个问题,但由于反馈不一,它有点卡住了。
目前这不在 JSON 模式的范围内。 @erosb 提到的提案仍在考虑中,但不适用于即将发布的 draft-07。如果有足够的需求,它可能会被考虑用于 draft-08。这将是项目范围的显着扩展,这就是为什么它在其他事情得到解决时被搁置的原因。
一些验证器可以让您轻松定义自己的扩展关键字,这可能是实现您想要的效果的好方法。肯定有一些库会应用 JSON 指针,让你知道它是否指向任何东西。如果你在某个地方实施@erosb 的提议,如果你能对这个问题发表评论并让我们知道它是如何运作的,那就太好了。
我有这个json:
{
"categories": [
{
"id": 1,
"name": "cat1",
"topics": [
{
"$ref": "#/topics/1"
}
]
}
],
"topics": [
{
"id": 1,
"name": "top1"
}
]
}
我已经编写了下一个模式来验证它:
{
"definitions": {
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"topics": {
"type": "array"
"items": { "$ref": "#/definitions/topic" }
}
}
},
"topic": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
},
"type": "object",
"properties": {
"categories": {
"items": { "$ref": "#/definitions/category" },
"type": "array"
},
"topics": {
"items": { "$ref": "#/definitions/topic" },
"type": "array"
}
}
}
当我在流行的在线验证器上使用此架构时,它不会捕获 #/topics/5
或 #/ttt/555
等无效引用。
我可以使用此架构来验证引用吗?你能推荐我图书馆或服务吗?
我不确定我是否正确理解了您想要实现的目标。我假设您想表示 "topics"
数组的项目应该是 JSON 引用("$ref"
和 JSON 指针)_并且指向的对象应该匹配模式 "#/definitions/topic"
.
如果是这种情况,那么目前没有办法用 json 模式来表达它,所以 - 即使是最新版本 - 你也只能表示一个字符串应该是 json 指针,但不能限制引用对象的类型。
去年我 made a suggestion 解决了这个问题,但由于反馈不一,它有点卡住了。
目前这不在 JSON 模式的范围内。 @erosb 提到的提案仍在考虑中,但不适用于即将发布的 draft-07。如果有足够的需求,它可能会被考虑用于 draft-08。这将是项目范围的显着扩展,这就是为什么它在其他事情得到解决时被搁置的原因。
一些验证器可以让您轻松定义自己的扩展关键字,这可能是实现您想要的效果的好方法。肯定有一些库会应用 JSON 指针,让你知道它是否指向任何东西。如果你在某个地方实施@erosb 的提议,如果你能对这个问题发表评论并让我们知道它是如何运作的,那就太好了。